如何在Android中居中对齐ActionBar标题?

Sou*_*ldi 91 android android-theme android-actionbar

我正在尝试使用以下代码将文本居中ActionBar,但它将自己对齐到左侧.

你如何让它出现在中心?

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("Canteen Home");
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(R.drawable.back);
Run Code Online (Sandbox Code Playgroud)

Ahm*_*mad 192

要在ABS中设置一个居中的标题(如果你想在默认情况下使用它ActionBar,只需删除方法名称中的"支持"),你可以这样做:

在您的Activity中,在您的onCreate()方法中:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout);
Run Code Online (Sandbox Code Playgroud)

abs_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
        android:layout_gravity="center"
    android:orientation="vertical">

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tvTitle"
        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#FFFFFF" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

现在你应该Actionbar只有一个标题.如果要设置自定义背景,请在上面的布局中设置它(但不要忘记设置android:layout_height="match_parent").

或者:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,结果是动作栏有点向右或向左移位,这取决于我显示的动作按钮.要修复它,我只需将layout_width设置为"WRAP_CONTENT" (13认同)
  • Pepillo的解决方案对我不起作用,因为内容根本不再集中.我可以通过将"android:layout_gravity ="center"添加到LinearLayout来解决这个问题. (11认同)
  • 如果有菜单项,它将无法正确居中.要真正始终以标题为中心将"WRAP_CONTENT"添加到LinearLayout并将android:layout_gravity ="center"从TextView移动到LinearLayout. (8认同)
  • 如何添加自定义后退按钮图像? (6认同)
  • @Nezam这是一种预期的行为.尝试`((TextView)actionBar.getCustomView().findViewById(R.id.textView1)).setText("new title");`,其中textView1是CustomView中的`TextView`的ID. (6认同)
  • 现在用`setTitle`改变标题 (2认同)
  • 如何在此代码中添加后退按钮?`getSupportActionBar().setDisplayHomeAsUpEnabled(true);`不工作 (2认同)

Som*_*ere 34

我在其他答案上没有取得多大成功......下面正是在Android 4.4.3上使用支持库v7中的ActionBar对我有用的.我把它设置为显示导航抽屉图标("汉堡菜单按钮")

XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/actionbar_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxLines="1"
        android:clickable="false"
        android:focusable="false"
        android:longClickable="false"
        android:textStyle="bold"
        android:textSize="18sp"
        android:textColor="#FFFFFF" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Java的

//Customize the ActionBar
final ActionBar abar = getSupportActionBar();
abar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));//line under the action bar
View viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_titletext_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
        ActionBar.LayoutParams.WRAP_CONTENT, 
        ActionBar.LayoutParams.MATCH_PARENT, 
        Gravity.CENTER);
TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
textviewTitle.setText("Test");
abar.setCustomView(viewActionBar, params);
abar.setDisplayShowCustomEnabled(true);
abar.setDisplayShowTitleEnabled(false);
abar.setDisplayHomeAsUpEnabled(true);
abar.setIcon(R.color.transparent);
abar.setHomeButtonEnabled(true);
Run Code Online (Sandbox Code Playgroud)


ypr*_*sto 9

用Sergii说,用标题文本定义你自己的自定义视图,然后将LayoutParams传递给setCustomView().

ActionBar actionBar = getSupportActionBar()
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
actionBar.setCustomView(getLayoutInflater().inflate(R.layout.action_bar_home, null),
        new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER
        )
);
Run Code Online (Sandbox Code Playgroud)

编辑:至少对于宽度,您应该使用WRAP_CONTENT或您的导航抽屉,应用程序图标等.不会显示(自定义视图显示在操作栏上的其他视图的顶部).特别是在未显示操作按钮时会发生这种情况.

编辑:在xml布局中等效:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical">
Run Code Online (Sandbox Code Playgroud)

这不需要指定LayoutParams.

actionBar.setCustomView(getLayoutInflater().inflate(R.layout.action_bar_home, null);
Run Code Online (Sandbox Code Playgroud)


Jor*_*rdy 8

只是艾哈迈德回答的快速补充.使用带TextView的自定义视图时,不能再使用getSupportActionBar().setTitle.因此,在您使用此自定义ActionBar(使用此xml)具有多个活动时设置标题,在分配自定义视图后的 onCreate()方法中:

TextView textViewTitle = (TextView) findViewById(R.id.mytext);
textViewTitle.setText(R.string.title_for_this_activity);
Run Code Online (Sandbox Code Playgroud)


小智 7

这里的代码为我工作。

    // 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);
}
Run Code Online (Sandbox Code Playgroud)


Din*_*ris 5

没有customview的话,它就能居中动作栏标题。它也非常适合导航抽屉

    int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
    TextView abTitle = (TextView) findViewById(titleId);
    abTitle.setTextColor(getResources().getColor(R.color.white));

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    abTitle.setGravity(Gravity.CENTER);
    abTitle.setWidth(metrics.widthPixels);
    getActionBar().setTitle("I am center now");
Run Code Online (Sandbox Code Playgroud)

快乐的编码。谢谢。