【问题标题】:Center title of activity in android action barandroid操作栏中活动的中心标题
【发布时间】:2013-07-11 23:34:08
【问题描述】:

现在我的活动标题在左侧显示为< Title,然后另一个菜单项显示在右侧。我想将我的标题居中并省略<。我怎么做?我正在使用我调用的典型菜单

public boolean onOptionsItemSelected(MenuItem item) 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.yesterday, menu);
    return true;
}

编辑:

我设法让它显示使用

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_yesterday);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,   R.layout.yesterday_title_bar);

yesterday_title_bar 是一个相对布局

但它显示了视图高度的一半。所以我创建了以下样式。但是当我在清单中应用样式时。清单说它找不到资源。

<style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">65dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>

清单:

<activity
        android:name="com.company.YesterdayActivity"
        android:theme="@android:style/CustomTheme" >
    </activity>

【问题讨论】:

  • 研究为ActionBar 扩展自定义布局。
  • 我试过了。它说不能将自定义与其他功能混合。
  • android.util.AndroidRuntimeException: You cannot combine custom titles with other title features 如何关闭它?
  • 你可以在这里发布你的自定义样式 xml 吗?
  • @user2045570 我发布风格

标签: android android-layout android-actionbar statusbar


【解决方案1】:

我也使用了actionbar sherlock,并通过这种方法设置了标题位置。您可以设置自定义文本视图并将其重心设置为中心。使用下面的方法告诉我。

    private void setHeader(){
    LayoutInflater linflater = (LayoutInflater)MainAccountActivity.context.getSystemService(MainAccountActivity.context.LAYOUT_INFLATER_SERVICE);
    View GalleryTitleView = linflater.inflate(R.layout.layout_header, null);

    galleryTitleTv = (TextView) GalleryTitleView
            .findViewById(R.id.GalleryTitleTv);
    galleryTitleTv.setText(ITWAppUIConstants.TAB_AGENDA);

    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.FILL_PARENT,
            ActionBar.LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
    GalleryTitleView.setLayoutParams(lp);
    getSupportActionBar().setCustomView(GalleryTitleView);
  }

这是我使用的布局:

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

<TextView
    android:id="@+id/GalleryTitleTv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingLeft="10dip"
    android:textColor="@color/white"
    android:textSize="20dip"
    android:textStyle="bold" />

【讨论】:

    【解决方案2】:

    主题不在 android: style 中,而是在 style 中。将您的清单更改为此..

        <activity
            android:name="com.company.YesterdayActivity"
            android:theme="@style/CustomTheme" >
        </activity>
    

    编辑: 也许这是你的父主题。你之前的主题是什么?尝试像这样更改您的父主题:

        <style name="CustomWindowTitleBackground">
            <item name="android:background">#323331</item>
        </style>
    
        <style name="CustomTheme" parent="@android:style/Theme.Holo.Light">
            <item name="android:windowTitleSize">65dip</item>
            <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
        </style>
    

    【讨论】:

    • 您知道如何让主题影响该 xml/activity 中的所有内容吗?我现在所有孩子的观点都不同了。例如,我的 EditText 更高。
    • 自定义主题会扭曲布局的其余部分。如果我将它从清单移动到特定的自定义布局,它甚至不会生效。
    • 它崩溃了:无法将自定义与其他功能结合起来
    【解决方案3】:

    只需在活动中为您的操作栏使用自定义视图...

    ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.actionbar_layout);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setDisplayHomeAsUpEnabled(false); // Remove '<' next to home icon.
    

    视图可能是这样的:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textViewActionBarTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your Title" />
    
    </LinearLayout>
    

    【讨论】:

    • 这很奇怪。实例化 actionBar 之后?
    • 是的。注意:我必须使用getActionBar() 而不是getSupportActionBar()
    • @CoteMounyo 老实说,我不记得为什么我把那条线留在了那里。只需将其删除。无论如何,标题将存在于您的自定义布局中。只需在那个 TextView 中设置它。但使用不支持的 ActionBar 可能存在问题。不知道为什么...
    • 另外,您需要设置DisplayOptions 以便它知道使用自定义视图...我刚刚编辑了原始帖子。