Android ActionBar透明背景

Android ActionBar透明背景,android,android-actionbar,android-xml,android-theme,Android,Android Actionbar,Android Xml,Android Theme,我已经创建了一个带有自定义xml布局的actionbar,它应该有一个透明的背景。它在ICS上正常工作,但在KitKat上它的背景是灰色的,而不是透明的。有谁能帮我解决这个问题,让actionbar对所有Android 4及以上版本都透明吗 以下是xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android

我已经创建了一个带有自定义xml布局的actionbar,它应该有一个透明的背景。它在ICS上正常工作,但在KitKat上它的背景是灰色的,而不是透明的。有谁能帮我解决这个问题,让actionbar对所有Android 4及以上版本都透明吗

以下是xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_actionbar_layout"
    android:background="@android:color/transparent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <ImageButton
        android:id="@+id/action_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:contentDescription="@string/app_name"
        android:padding="5dp"
        android:src="@drawable/menu" />

    <ImageButton
        android:id="@+id/action_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:contentDescription="@string/app_name"
        android:padding="5dp"
        android:src="@drawable/settings" />

    <ImageButton
        android:id="@+id/action_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/action_settings"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/action_settings"
        android:background="@android:color/transparent"
        android:contentDescription="@string/app_name"
        android:padding="5dp"
        android:src="@drawable/share" />

    <com.quanticapps.athan.views.RobotoTextView
        android:id="@+id/action_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/app_name"
        android:textColor="@android:color/white" />

</RelativeLayout>
试试这些方法

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff")));

最好以样式设置Actionbar属性。 定义将actionbar背景设置为透明的样式,并将该样式应用于您的活动。这样,您的代码将不需要处理UI和逻辑

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff")));