android - Sherlock actionBar.setIcon(android.R.color.transparent) 向左留空

标签 android actionbarsherlock

我尝试在 Google 和 stackoverflow 上搜索,但没有找到解决方案。

问题:

gap_img

代码:

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

但结果是:

enter image description here

请帮忙!!!

[编辑]:

我正在使用 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>

enter image description here

如您所见,文本不在中心,有什么建议吗???

最佳答案

不是图标,而是一个 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/

相关文章:

android - 项目 'HelloFacebookSample' 缺少必需的 Java 项目 'facebook'

java - Activity 关闭并重新启动后计时器不会停止

android - menu.xml 中的 ActionbarSherlock SearchView 给出 Resources$NotFoundException

Android 2.X 中的 Android ListView 问题

android - ActionBarSherlock:通过方法调用以编程方式显示/隐藏操作项按钮

java - 如何获取所有应用程序用户的位置并将其显示在 map 上

Android KeyBoard 不显示在 AlertDialog 中

android - setRequestedOrientation 横向加载纵向布局

android - 带有搜索 View 的操作栏。反向兼容问题

android-layout - 如何在 ICS 样式的 EditText 中更改线条的颜色