更改sherlock操作栏菜单项背景颜色在android中

时间:2013-04-27 17:08:04

标签: android menu actionbarsherlock

我正在使用sherlocklibrary在我的应用程序中显示操作栏,它工作正常但是当我为标题添加背景图像时,右侧会出现黑色区域​​(请参阅快照)。 如何更改菜单按钮的背景颜色或至少填充标题图像的父级。 我会根据用户的选择在运行时更新此图像。enter image description here 这是我的标题栏布局。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/headerLinear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >


    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
         />

    <TextView
        android:id="@+id/title"
        style="@style/titleStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/logo"
        android:maxLength="30"
        android:ellipsize="end"
        android:text="@string/login_en" />

</RelativeLayout>

我把这段代码放在我的活动中:

            BitmapDrawable bg = (BitmapDrawable)  getResources().getDrawable(R.drawable.bg_striped_split);

    getSupportActionBar().setBackgroundDrawable(bg);

    LayoutInflater inflater = LayoutInflater.from(this);
    View customView = inflater.inflate(R.layout.title_bar, null);

    RelativeLayout header = (RelativeLayout) customView.findViewById(R.id.headerLinear);



    header.setBackgroundDrawable(R.drawable.header);  

1 个答案:

答案 0 :(得分:0)

我尝试了这个,它从右侧移除黑色区域外观.. 检查完整代码here

        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        // ActionBar actionBar = getSupportActionBar();
        getSupportActionBar().setCustomView(R.layout.actionbar_top); // load
                                                                        // your
                                                                        // layout
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

图像

enter image description here

相关问题