【问题标题】:ActionBar transparent background动作条透明背景
【发布时间】:2014-06-14 09:27:07
【问题描述】:

我创建了一个带有自定义 xml 布局的操作栏,该布局应该具有透明背景。它在 ICS 上正常工作,但在 KitKat 上它有灰色背景而不是透明度。谁能帮我解决这个问题并使操作栏对所有 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>

这是我使用的代码:

final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater()
                .inflate(R.layout.main_actionbar, null);
        // Set up your ActionBar
        final ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(actionBarLayout);
        // Typeface mTypeface = Typeface.createFromAsset(getAssets(),
        // "rockwell.ttf");
        title = (TextView) findViewById(R.id.action_title);
        share = (ImageButton) findViewById(R.id.action_share);
        settings = (ImageButton) findViewById(R.id.action_settings);
        menu = (ImageButton) findViewById(R.id.action_menu);

        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);

        title.setTextSize(15);
        title.setGravity(Gravity.CENTER);

【问题讨论】:

    标签: android android-actionbar android-xml android-theme


    【解决方案1】:

    试试这些方法..

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

    check this answer

    【讨论】:

      【解决方案2】:

      最好在样式中设置 Actionbar 属性。 定义一种将操作栏背景设置为透明的样式,并将该样式应用于您的活动。这样,您的代码将无需处理 UI 和逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-08
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 2018-07-06
        相关资源
        最近更新 更多