我尝试在 Google 和 stackoverflow 上搜索,但没有找到解决方案。
问题:
代码:
actionBar = activity.getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
//actionBar.setDisplayUseLogoEnabled(false);
//actionBar.setDisplayHomeAsUpEnabled(false);
//actionBar.setDisplayShowHomeEnabled(false);
actionBar.setIcon(android.R.color.transparent);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
LayoutInflater inflator = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout v = (LinearLayout) inflator.inflate(R.layout.header, null);
actionBar.setCustomView(v, lp);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/topheaderbackground" >
<TextView
android:id="@+id/topcaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal"
android:text="ABC"
android:textColor="#ebf5ff"
android:textSize="9.6pt"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
我尝试过的:
actionBar.setDisplayShowHomeEnabled(false);
但结果是:
请帮忙!!!
[编辑]:
我正在使用 Theme.Sherlock
。
[Doctoror Drive的建议结果]:
actionBar.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.topheaderbackground));
新的header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/topcaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal"
android:text="ABC"
android:textColor="#ebf5ff"
android:textSize="9.6pt"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
如您所见,文本不在中心,有什么建议吗???
最佳答案
不是图标,而是一个 CustomView 留下了空隙。 您应该在样式中更改 ActionBar 的颜色。
http://developer.android.com/guide/topics/ui/actionbar.html#AdvancedStyles
然后添加没有背景的自定义 View ,中间只有一个 TextView。
编辑: 将其设置为居中后,文本的空间是操作栏内容的空间减去左侧图标(透明)的大小。尝试设置 ActionBar 显示选项。 删除
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
并改为添加
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
如果您还有问题,请告诉我,然后我会告诉 B 计划。
编辑,B 计划:
好的,设置上面的内容后翻转会出现问题,因此请尝试向 TextView 的根部添加填充。设置 ActionBar 模式:
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM |
ActionBar.DISPLAY_SHOW_HOME);
而在xml中,我认为那里的LinearLayout是没用的。尝试设置简单干净的 TextView
<TextView
android:id="@+id/topcaption"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="?android:attr/actionBarSize"
android:gravity="center"
android:text="ABC"
android:textColor="#ebf5ff"
android:textSize="9.6pt"
android:textStyle="bold"
android:typeface="sans" />
如果不行,试试
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="?android:attr/actionBarSize"
android:orientation="horizontal">
<TextView
android:id="@+id/topcaption"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="ABC"
android:textColor="#ebf5ff"
android:textSize="9.6pt"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
关于android - Sherlock actionBar.setIcon(android.R.color.transparent) 向左留空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20348404/