【问题标题】:Center Align title in action bar using styles in android.使用 android 中的样式在操作栏中居中对齐标题。
【发布时间】:2013-10-15 04:21:44
【问题描述】:

您好,我正在开发安卓应用程序。在我的应用程序中,我正在使用操作栏。我想在操作栏中使我的窗口标题居中对齐。我知道可以使用 xml 文件。但我想使用样式来做到这一点,以便它适用于我的每个窗口,如何做到这一点需要帮助。谢谢。

【问题讨论】:

标签: android android-actionbar title center-align


【解决方案1】:

无需为 actionBar 使用自定义布局即可将 ActionBarTitle 居中的简单方法。

但在此之前先将图标隐藏为:

myActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

private void centerActionBarTitle() {
    int titleId = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        titleId = getResources().getIdentifier("action_bar_title", "id", "android");
    } else {
        // This is the id is from your app's generated R class when 
        // ActionBarActivity is used for SupportActionBar
        titleId = R.id.action_bar_title;
    }

    // Final check for non-zero invalid id
    if (titleId > 0) {
        TextView titleTextView = (TextView) findViewById(titleId);
        DisplayMetrics metrics = getResources().getDisplayMetrics();

        // Fetch layout parameters of titleTextView 
        // (LinearLayout.LayoutParams : Info from HierarchyViewer)
        LinearLayout.LayoutParams txvPars = (LayoutParams) titleTextView.getLayoutParams();
        txvPars.gravity = Gravity.CENTER_HORIZONTAL;
        txvPars.width = metrics.widthPixels;
        titleTextView.setLayoutParams(txvPars);
        titleTextView.setGravity(Gravity.CENTER);
    }
}

【讨论】:

  • 对 action_bar_subtitle 重复同样的操作也会使 Action Bar 字幕居中。
  • 有效,但如果 ActionBar 有 MenuItems,标题会向左移动一些像素。
  • 如果这样做后标题有点偏离中心,只需使用 titleTextView 左填充。
  • 我应该在哪里调用这个方法?我正在调用 onCreate,作为最后一条指令,但我遇到了崩溃
  • 这似乎对我不起作用。我不断为titleTextView 获取null。我尝试在 onCreate、onResume 中调用此方法,甚至制作了一个 UI 按钮来触发此方法,但它们最终都一样。我做错了什么?
【解决方案2】:

我知道使用 xml 文件是可能的。但我想用风格来做 以便它适用于我的每个窗口,如何做到这一点 帮助。

您仍然使用 xml 来执行此操作。只需将样式应用程序广泛应用,而不仅仅是单个活动。请参阅 android 文档以获取完整说明:

https://developer.android.com/training/basics/actionbar/styling.html

然后将您的主题应用到您的整个应用或单个活动:

<application android:theme="@style/CustomActionBarTheme" ... />

编辑:以下是您必须如何处理标题居中的问题:

在你的 action_bar.xml 中你会有这样的东西:

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="SOME TITLE"
    android:textSize="18sp" />
</RelativeLayout>

然后在你的 onCreate() 中你会有类似的东西:

getSupportActionBar().setCustomView(R.layout.action_bar);

如果您需要在每项活动中使用此功能,您有 2 个选择:

  1. 蛮力方式:将上面这行代码放在每个Activity的onCreate中。
  2. 扩展Activity并将你的ActionBar初始化代码放在onCreate中。然后为每个 Activity 扩展您的自定义 Activity 类。只是不要忘记在每个 Activity 的 onCreate() 中调用 super()。

【讨论】:

  • 感谢您的帮助我检查了您给定的链接。实际上我可以设置标题的颜色大小但不能进行发件人对齐。你对此有任何想法。需要帮忙。谢谢。
  • 更新了我的答案。希望对你有帮助。
  • 感谢您的帮助。我也是这样做的。但我仍然觉得我不允许通过样式来集中它。:(
  • 我在 style.xml 中的自定义样式是否也会影响此自定义操作栏?
【解决方案3】:

很遗憾,SBerg413 的方法对我不起作用。

我的自定义布局是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/custom_action_bar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="test"
        android:textColor="@color/action_bar_title_color"
        android:textSize="@dimen/action_bar_title_size" />
</RelativeLayout>

一切都很完美:

UPD:我将这个活动用作我所有活动的父项,它应该有操作栏。

public class ActionBarCustomizationActivity extends ActionBarActivity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.getSupportActionBar().setDisplayShowCustomEnabled(true);

        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.custom_action_bar, null);

        TextView titleTextView = (TextView) v.findViewById(R.id.custom_action_bar_title);
        titleTextView.setText(this.getTitle());
        titleTextView.setTypeface(App.getInstance().getActionBarTypeFace());

        this.getSupportActionBar().setCustomView(v);
    }
}

【讨论】:

    【解决方案4】:

    @Napolean 的回答帮助了我,但就我而言,我使用的是 Xamarin android (monodroid)。以下是他的 C# 代码翻译:

    var titleId = Resources.GetIdentifier("action_bar_title", "id", "android");
    var titleTextView = FindViewById<TextView>(titleId);
    var layoutParams = (LinearLayout.LayoutParams) titleTextView.LayoutParameters;
    layoutParams.Gravity = GravityFlags.CenterHorizontal;
    layoutParams.Width = Resources.DisplayMetrics.WidthPixels;
    titleTextView.LayoutParameters = layoutParams;
    titleTextView.Gravity = GravityFlags.Center;
    //Added padding because it is slightly off centered.
    titleTextView.SetPadding(100, 0,0,0);
    

    【讨论】:

    • 您从哪里调用此代码?我已经从主要活动中完成了它,但是当 OnConfigurationChange 从旋转中触发时,居中会被重置。
    猜你喜欢
    • 2014-07-21
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 2011-07-07
    • 2013-01-13
    相关资源
    最近更新 更多