关键词:
【中文标题】如何在默认主题中对齐 ActionBar 中心的标题(Theme.Holo.Light)【英文标题】:How to align title at center of ActionBar in default theme(Theme.Holo.Light) 【发布时间】:2013-08-27 10:30:46 【问题描述】:我搜索了很多但找不到方法,如何将标题设置在 ActionBar 的中心而不是左对齐。我使用下面的代码将标题设置在中心:
ViewGroup decorView= (ViewGroup) this.getWindow().getDecorView();
LinearLayout root= (LinearLayout) decorView.getChildAt(0);
FrameLayout titleContainer= (FrameLayout) root.getChildAt(0);
TextView title= (TextView) titleContainer.getChildAt(0);
title.setGravity(Gravity.CENTER);
但它给出的错误如下:
ClassCastException : com.android.internal.widget.ActionBarView can not
be cast to android.widget.TextView.
任何其他解决方案..任何帮助将不胜感激。
【问题讨论】:
你想在 Android 上模仿 iOS 设计吗?因为你真的不应该:) 【参考方案1】:您可以创建自定义布局并将其应用到 actionBar。
为此,请执行以下 2 个简单步骤:
Java 代码
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
其中R.layout.actionbar
是以下布局。
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_
android:layout_
android:layout_gravity="center"
android:id="@+id/action_bar_title"
android:text="YOUR ACTIVITY TITLE"
android:textColor="#ffffff"
android:textSize="24sp" />
</LinearLayout>
它可以像你想要的那样复杂。试试看!
编辑:
要设置background
,您可以在容器布局中使用属性android:background
(在这种情况下为LinearLayout)。您可能需要将布局高度android:layout_height
设置为match_parent
而不是wrap_content
。
此外,您还可以为其添加一个LOGO / ICON。为此,只需在布局中添加一个ImageView,并将布局方向属性android:orientation
设置为水平(或简单地使用RelativeLayout 并自行管理)。
要动态更改上述自定义操作栏的标题,请执行以下操作:
TextView title=(TextView)findViewById(getResources().getIdentifier("action_bar_title", "id", getPackageName()));
title.setText("Your Text Here");
【讨论】:
它工作完美,但它使用自定义显示..我正在使用 Theme.Holo.Light..所以所有带有图像、背景颜色和后退按钮的徽标都被删除,只有简单的白色标题栏是显示在 ActionBar 的中心。有没有办法在我当前主题的中心显示它?谢谢。 不,你的第二个答案不起作用..它说 FrameLayout 无法解析输入。 它工作完美,但它使用自定义显示..我正在使用 Theme.Holo.Light..所以所有带有图像、背景颜色和后退按钮的徽标都被删除,只有简单的白色标题栏是显示在 ActionBar 的中心。 我想在默认主题中使用此功能。我不想在 ActionBar.DISPLAY_SHOW_CUSTOM 中使用它。所以它不起作用。 如果您使用支持ActionBar
,请务必使用getSupportActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
。如果不使用,仅使用 ActionBar.DISPLAY_SHOW_CUSTOM
可能会显示红色编译错误。【参考方案2】:
如果没有自定义视图,似乎没有办法做到这一点。可以获取标题视图:
View decor = getWindow().getDecorView();
TextView title = (TextView) decor.findViewById(getResources().getIdentifier("action_bar_title", "id", "android"));
但是更改gravity
或layout_gravity
没有效果。
ActionBarView
中的问题,它自己布局其子级,因此更改其子级的布局参数也没有效果。
要查看此执行以下代码:
ViewGroup actionBar = (ViewGroup) decor.findViewById(getResources().getIdentifier("action_bar", "id", "android"));
View v = actionBar.getChildAt(0);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.gravity= Gravity.CENTER;
v.setLayoutParams(p);
v.setBackgroundColor(Color.BLACK);
【讨论】:
actionBar Apptheme 的空指针异常是 DarkActionBar【参考方案3】:查看 Lollipop 更新中支持库类的新工具栏,您可以通过在布局中添加工具栏来设计操作栏
在您的应用主题中添加这些项目
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
在布局中创建您的工具栏,并将您的文本视图包含在您的工具栏的中心设计中
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="@color/acbarcolor">
<RelativeLayout
android:layout_
android:layout_ >
<TextView
android:id="@+id/toolbar_title"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/app_name"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
将您的操作栏添加为工具栏
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null)
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
请确保您需要像这样在资源文件中包含工具栏
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<include
android:layout_
android:layout_
layout="@layout/toolbar" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_
android:layout_>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_
android:layout_>
<include
android:layout_
android:layout_
layout="@layout/homepageinc" />
</FrameLayout>
<fragment
android:id="@+id/fragment1"
android:layout_gravity="start"
android:name="com.shouldeye.homepages.HomeFragment"
android:layout_
android:layout_ />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
【讨论】:
【参考方案4】:我知道我的回答不及时,但这纯粹是代码,不需要xml
。
这是用于Activity
public void setTitle(String title)
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView textView = new TextView(this);
textView.setText(title);
textView.setTextSize(20);
textView.setTypeface(null, Typeface.BOLD);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(getResources().getColor(R.color.white));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(textView);
这是用于Fragment
public void setTitle(String title)
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView textView = new TextView(getActivity());
textView.setText(title);
textView.setTextSize(20);
textView.setTypeface(null, Typeface.BOLD);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(getResources().getColor(R.color.white));
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(textView);
【讨论】:
超级简单的实现和非常容易定制。不错!【参考方案5】:如果您正在使用工具栏,只需添加
android:layout_gravity="center_horizontal"
工具栏下 就这样sn-p
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/tvTitle"
android:layout_
android:layout_
android:layout_gravity="center_horizontal" //Important
android:textColor="@color/whitecolor"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
【讨论】:
如果您正在使用工具栏,希望对大家有所帮助:)【参考方案6】:这确实有效:
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.custom_actionbar);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.gravity = Gravity.CENTER;
您必须根据您的要求定义 custom_actionbar.xml 布局,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#2e2e2e"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_
android:layout_
android:src="@drawable/top_banner"
android:layout_gravity="center"
/>
</LinearLayout>
【讨论】:
【参考方案7】:我认为通过在操作栏上定位视图,您将达到您想要的。在操作栏的布局上,当布局参数设置为匹配父级时,它与全宽不匹配 这就是为什么我在成功的地方以编程方式完成它。通过设置宽度(它的工作方式类似于 layout_weight="value"),我们可以在任何我们想要的地方设置我们的视图:
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater ll = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) ll.inflate(R.layout.custom_actionbar, null);
//inner layout within above linear layout
LinearLayout inner = (LinearLayout) linearLayout.findViewById(R.id.inner_linear_ractionbar);
inner.setMinimumWidth(getWidth() * 2 / 10);
// we will set our textview to center and display there some text
TextView t = (TextView) linearLayout.findViewById(R.id.center_text);
t.setWidth(getWidth() * 6 / 10);
Button b = (Button) linearLayout.findViewById(R.id.edit_button);
b.setWidth(getWidth() *3/ 20);
ImageButton imageButton = (ImageButton) linearLayout.findViewById(R.id.action_bar_delete_item);
imageButton.setMinimumWidth(deviceUtils.getWidth() / 20);
这里是getWidth()方法:
public int getWidth()
DisplayMetrics dm = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
【讨论】:
【参考方案8】:Java 代码:
写在 onCreate()getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.action_bar);
对于您的自定义视图,只需使用 FrameLayout,East peasy! android.support.v7.widget.Toolbar 是另一种选择
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<TextView
android:layout_
android:layout_
android:layout_gravity="left|center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/app_name"
android:textColor="@color/black"
android:id="@+id/textView" />
</FrameLayout>
【讨论】:
【参考方案9】:我遇到了同样的问题,由于工具栏中自动添加了“主页”按钮,我的文本没有准确输入。
我以肮脏的方式修复了它,但在我的情况下效果很好。我只是在 TextView 的右侧添加了一个边距,以补偿左侧的主页按钮。这是我的工具栏布局:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="1dp"
android:id="@+id/toolbar"
android:layout_
android:layout_
app:layout_collapseMode="pin"
android:gravity="center"
android:background="@color/mainBackgroundColor"
android:fitsSystemWindows="true" >
<com.lunabee.common.utils.LunabeeShadowTextView
android:id="@+id/title"
android:layout_
android:layout_
android:layout_marginRight="?attr/actionBarSize"
android:gravity="center"
style="@style/navigation.toolbar.title" />
</android.support.v7.widget.Toolbar>
【讨论】:
【参考方案10】:您可以通过包装标题和级别右填充来强制工具栏,该填充具有默认的标题左填充。比将背景颜色添加到工具栏的父级,并且通过包装标题剪切的部分颜色相同(在我的示例中为白色):
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_
android:layout_
android:background="@color/white">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
android:paddingEnd="15dp"
android:paddingRight="15dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:titleTextColor="@color/black"/>
</android.support.design.widget.AppBarLayout>
【讨论】:
【参考方案11】:解决方案基于以下几点:
您需要使用自己的扩展工具栏的类 (support.v7.widget.toolbar) 您需要覆盖一种方法 Toolbar#addView它是做什么的:
当第一次工具栏执行#setTitle 时,它会创建 AppCompatTextView 并使用它来显示标题文本。
当 AppCompatTextView 创建时,工具栏(作为 ViewGroup)使用#addView 方法将其添加到自己的层次结构中。
另外,在尝试寻找解决方案时,我注意到 textview 的布局宽度设置为“wrap_content”,因此我决定将其设为“match_parent”并将 textalignment 分配给“center”。
MyToolbar.kt,跳过不相关的东西(构造函数/导入):
class MyToolbar : Toolbar
override fun addView(child: View, params: ViewGroup.LayoutParams)
if (child is TextView)
params.width = ViewGroup.LayoutParams.MATCH_PARENT
child.textAlignment= View.TEXT_ALIGNMENT_CENTER
super.addView(child, params)
可能的“副作用” - 这也适用于“字幕”
【讨论】:
【参考方案12】:在浏览了两天之后,这就是我在 Kotlin 中想到的。经过测试并在我的应用上运行
private fun setupActionBar()
supportActionBar?.apply
displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
displayOptions = ActionBar.DISPLAY_SHOW_TITLE
setDisplayShowCustomEnabled(true)
title = ""
val titleTextView = AppCompatTextView(this@MainActivity)
titleTextView.text = getText(R.string.app_name)
titleTextView.setSingleLine()
titleTextView.textSize = 24f
titleTextView.setTextColor(ContextCompat.getColor(this@MainActivity, R.color.appWhite))
val layoutParams = ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT
)
layoutParams.gravity = Gravity.CENTER
setCustomView(titleTextView, layoutParams)
setBackgroundDrawable(
ColorDrawable(
ContextCompat.getColor(
this@MainActivity,
R.color.appDarkBlue
)
)
)
【讨论】:
将 ActionBar 标题与 Xamarin 中心对齐
】将ActionBar标题与Xamarin中心对齐【英文标题】:AlignActionBarTitletocenterXamarin【发布时间】:2016-03-1611:37:07【问题描述】:我正在使用XamarinForms,我删除了我的图标,现在我想将操作栏标题放在中心,实际上它在左侧。我看到一些... 查看详情
android如何隐藏actionbar,保留标题栏
actionBar=getActionBar(); actionBar.hide();把标题栏都给隐藏了。。。。。。。。。肿么办?在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提... 查看详情
如何在Android中居中&左&右对齐ActionBar图标
】如何在Android中居中&左&右对齐ActionBar图标【英文标题】:Howtocenter&left&rightaligntheActionBariconsinAndroid【发布时间】:2015-09-1621:07:24【问题描述】:您好,我想在工具栏中对齐一组图标,如下图所示。我用谷歌搜索了一... 查看详情
使用 Bootstrap 主题和在页面中心对齐表单
】使用Bootstrap主题和在页面中心对齐表单【英文标题】:UsingBootstrapThemeandAligningForminCenterofPage【发布时间】:2018-06-2310:17:42【问题描述】:我想将表单置于页面中心(例如在Google主页上),但这样做时遇到了问题。我正在使用Boo... 查看详情
如何在 div 中水平居中对齐 ul
】如何在div中水平居中对齐ul【英文标题】:Howtohorizontallycentrealignaulwithinadiv【发布时间】:2017-01-0521:26:56【问题描述】:我不确定为什么这个文本对齐中心没有生效。我查看了其他教程,我将textalign:center应用于div,然后将display... 查看详情
如何使用 GridBagLayout 在 JPanel 中对齐组件中心?
】如何使用GridBagLayout在JPanel中对齐组件中心?【英文标题】:HowtoaligncomponentscenterintheJPanelusingGridBagLayout?【发布时间】:2013-06-1016:38:23【问题描述】:当我尝试对齐我的组件时,它要么向左要么向右。所以我只是想要解决这个问... 查看详情
如何在 Flutter 屏幕中心的列表视图中对齐所有小部件?
】如何在Flutter屏幕中心的列表视图中对齐所有小部件?【英文标题】:HowdoIalignallthewidgetswithinlistviewatthecenterofthescreeninFlutter?【发布时间】:2021-12-1317:09:47【问题描述】:在同一个正文中,我正在尝试将我的所有小部件对齐在我... 查看详情
如何在自定义按钮中对齐中心文本?
】如何在自定义按钮中对齐中心文本?【英文标题】:Howaligncentertextincustombuttons?【发布时间】:2013-05-1601:47:21【问题描述】:我有这个html<head><title>HTML5App</title><linkrel="stylesheet"type="text/css"href="css/custom.css"><lin... 查看详情
如何在 WKWebView 目标 c 中对齐图像中心
】如何在WKWebView目标c中对齐图像中心【英文标题】:HowtoalignanimageinthecenterinWKWebViewobjectivec【发布时间】:2018-08-0810:02:36【问题描述】:这是我显示图像的代码。NSString*doc=@"http://13.232.1.87:3000/images/flights/bc4952c8ec51588cdbd72d91c4e1533562... 查看详情
如何使材质 UI 选项卡标签离开?默认情况下它的中心对齐
】如何使材质UI选项卡标签离开?默认情况下它的中心对齐【英文标题】:HowtomakematerilUItabslabelleft?bydefaultitscenteraligned【发布时间】:2020-08-0603:13:39【问题描述】:默认材质ui标签标签是居中对齐的,如何让它左对齐?centeralignedt... 查看详情
在 AppCompat 21 中使用 Light.DarkActionBar 主题更改 ActionBar 标题文本颜色
】在AppCompat21中使用Light.DarkActionBar主题更改ActionBar标题文本颜色【英文标题】:ChangeActionBartitletextcolorusingLight.DarkActionBarthemeinAppCompat21【发布时间】:2015-01-0808:16:08【问题描述】:我正在使用v7appcompat21库在棒棒糖之前的设备上使... 查看详情
如何隐藏默认片段 actionBar 在 android & kotlin 中创建我们自己的 actionBar
】如何隐藏默认片段actionBar在android&kotlin中创建我们自己的actionBar【英文标题】:HowtohidethedefaultfragmentactionBarcreateourownactionBarinandroid&kotlin【发布时间】:2021-11-0620:18:27【问题描述】:我有一个底部导航的android应用程序,其... 查看详情
如何左对齐和垂直对齐标题中的图像并将文本水平保持在div的中心[重复]
】如何左对齐和垂直对齐标题中的图像并将文本水平保持在div的中心[重复]【英文标题】:Howtoleftalignandverticallyalignaimageinaheaderandkeeptextincenterofdivhorizontally[duplicate]【发布时间】:2020-02-2507:58:39【问题描述】:我正在寻求帮助,在... 查看详情
如何垂直对齐段落中心
】如何垂直对齐段落中心【英文标题】:HowtoalignaParagraphcentrevertically【发布时间】:2020-10-2220:16:52【问题描述】:我在页面中有一个小段落,我想将其水平居中和垂直居中对齐。水平对齐中心非常简单,但是我正在努力寻找一种... 查看详情
垂直对齐除中心以外的按钮的内容
...间】:2014-11-2709:49:45【问题描述】:通常人们试图弄清楚如何将内容垂直居中,我想删除居中内容的实例并对齐,但我被卡住了。默认情况下,按钮的内容(放置在表格单元格的列表中)垂直居中。我怎样才能删除这个?如何将... 查看详情
如何在页面中心的多个面板中仅对齐两个面板的分组文本?
】如何在页面中心的多个面板中仅对齐两个面板的分组文本?【英文标题】:HowtoalignGroupingTextofonlytwopanelsoutofmanyPanelsinapagecenter?【发布时间】:2017-09-0911:00:51【问题描述】:在我的页面中有许多面板和分组文本。对于两个面板,... 查看详情
androidtoolbar的详细使用步骤
...tion标签下Theme属性3.自定义Theme属性,因为Activity默认是有ActionBar的,所以需要先将默认的ActionBar去掉(parent="Theme.AppCompat.Light.NoActionBar"),并根据项目需求选择主题的每个特定的属性附录一张常用属性图,上面的每个属性就很好理... 查看详情
如何在约束布局中将一个视图与另一个视图的中心对齐
】如何在约束布局中将一个视图与另一个视图的中心对齐【英文标题】:Howtoalignoneviewtothecenterofanotherviewinconstraintlayout【发布时间】:2021-09-2820:59:14【问题描述】:在上图中,我必须将这些点(在XML中只是空视图)对齐到其上方... 查看详情