【问题标题】:Custom ActionBar title in Android not centering?Android中的自定义ActionBar标题不居中?
【发布时间】:2014-06-17 17:23:32
【问题描述】:

我的 ActionBar 有一个自定义布局。这是一个带有今天日期的单个 TextView 的 LinearLayout。 TextView 完全以 4.0+ 设备为中心,甚至在 3.0 模拟器上。但它在 3.2 模拟器中有点偏离中心:

我在我的主要活动中这样做:

getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getActionBar().setCustomView(R.layout.actionbar_custom);
dateTextView = (TextView) findViewById(R.id.dateTextView);

这里是actionbar_custom.xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center_vertical|center_horizontal" >

    <TextView 
        android:id="@+id/dateTextView"      
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center"
        android:gravity="center_vertical|center_horizontal" />
    
</LinearLayout>

看起来溢出按钮占用了布局空间,这可能是日期未居中的原因。但是在我的 4.0+ 设备上,溢出图标存在,但日期完全居中,所以我很困惑。

更新:

我想我找到了问题的根源。我在我的 Application 类中使用了一种解决方法来强制溢出按钮出现在具有菜单键的设备上:

try 
{
    ViewConfiguration config = ViewConfiguration.get(this);
    Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
    if(menuKeyField != null) 
    {
        menuKeyField.setAccessible(true);
        menuKeyField.setBoolean(config, false);
    }
} 
catch (Exception ex) 
{
    // Ignore
}

这似乎是在强制溢出图标出现在自己的布局中。

有没有办法将我的 TextView 放在这些设备上 ActionBar 的整体父布局中?这样我就可以将 TextView 居中。

【问题讨论】:

  • @Raghunandan 在启用了“硬件键”的模拟器中(比如我的 3.0 模拟器),溢出图标不存在,但文本居中。
  • 根据仪表板,android 版本 3.X 不到 0.1%。不要浪费你的时间developer.android.com/about/dashboards/…
  • @LordRaydenMK 我真的很想这样做,因为为 3.x 设备自定义 ActionBar 布局简直就是一场噩梦 :)
  • 这对我不起作用。

标签: android android-layout android-actionbar


【解决方案1】:

最简单的解决方案是使用相对布局并将 centerInParent = "true" 添加到您的文本视图中。

【讨论】:

  • 感谢您的回复,但我试过了,它仍然处于相同的位置。您还有其他想法吗?
猜你喜欢
  • 1970-01-01
  • 2012-07-18
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
相关资源
最近更新 更多