android - Android 操作栏中 Activity 的中心标题

标签 android android-layout android-actionbar statusbar

现在我的 Activity 标题在左侧显示为 < 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是一个relativeLayout

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

<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>

list :

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

最佳答案

我还使用了操作栏夏洛克,并通过这种方法设置标题位置。您可以设置自定义 TextView 并将其重力设置为中心。使用下面的方法并告诉我。

    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" />

关于android - Android 操作栏中 Activity 的中心标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17605282/

相关文章:

android - 如何在Android上获取屏幕工作区的高度

android - 无法使子布局在 ScrollView 中填充其父布局

android - 如何在不弄乱布局的情况下隐藏 TextView 和 Button?

android - 将 Android 操作栏与菜单选项卡合并

android - 开发 android 应用程序需要哪个框架? MVC/MVP/MVVM

android - 我有一个 EditText 字段,我想在应用程序再次打开时保存它

android - Google Play 服务 7.5.0 AnalyticsService NPE onStartCommand

android - 如何将一个元素对齐到相对布局中另一个元素的中心和上方?

ios - 如何删除 ActionBar 上 ActionContent 和 NavigationContent 的边框