50

我一直在淘汰interwebs(例如Android文檔,這裏的答案等),以回答我認爲會是一個相當微不足道的問題。你如何獲得像Google MusicYouTube那樣的半透明操作欄?(鏈接是圖像示例)?自定義半透明的Android ActionBar

我希望視頻內容能夠全屏顯示,不受操作欄的限制/推下,同時仍然充分利用內置UI組件的優點。我顯然可以使用完全自定義的視圖,但如果可能的話,我寧願使用ActionBar。

艙單

<activity 
    android:name="videoplayer" 
    android:theme="@android:style/Theme.Holo" 
    android:launchMode="singleTop" 
    android:configChanges="keyboardHidden|orientation"/> 

活動

// Setup action bar 
ActionBar actionBar = getActionBar(); 
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
View customActionBarView = getLayoutInflater().inflate(R.layout.player_custom_action_bar, null); 
actionBar.setCustomView(customActionBarView, 
         new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, 
                R.dimen.action_bar_height)); 
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

菜單

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/button1" 
     android:icon="@drawable/button1" 
     android:showAsAction="always"/> 

    <item 
     android:id="@+id/button2" 
     android:icon="@drawable/button2" 
     android:showAsAction="always"/> 
</menu> 

自定義動作條查看

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/custom_action_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="center" 
    android:background="@color/TranslucentBlack"> 

    <!--Home icon with arrow--> 
    <ImageView 
     android:id="@+id/home" 
     android:src="@drawable/home" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent"/> 

    <!--Another image--> 
    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:visibility="visible"/> 

    <!--Text if no image--> 
    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     android:visibility="gone"/> 

    <!--Title--> 
    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:paddingLeft="20dp" 
     android:gravity="center_vertical"/> 
</LinearLayout> 
+0

在這裏找到答案: http://stackoverflow.com/questions/16939814/android-util-androidruntimeexception-requestfeature-must-be-called-before-add – user3167086

回答

100

如果你希望你的活動進行全屏,但仍然表現出動作條,但您有權要求overlaymode的動作條在你的活動onCreate()阿爾法:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 

//getWindow().requestFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY); << Use this for API 7+ (v7 support library) 

Then after you called setContentView(..)(因爲setContentView(..)也會初始化設置內容旁邊的操作欄),您可以在操作欄上設置可拖動的背景:

getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg)); 

它可以是一個形狀繪製與阿爾法放在RES /繪製:通過創建ColorDrawable

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#BB000000" /> 
</shape> 

你也可以這樣做完全編程:

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0))); 

否則ofcourse你可以完全隱藏操作欄;通過調用

getActionBar().hide(); 
+0

謝謝!我覺得這是我錯過的一些簡單的事情。 – colabug

+0

你知道如何調整動作欄的高度嗎?我的維度似乎沒有效果! 'actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, R.dimen.action_bar_height));' – colabug

+0

即時通訊不知道你是否真的可以改變操作欄的高度,我猜不是,但是如果可能的話,我建議不要這樣做,因爲它是蜂窩中的一個明確的UI組件。 – MrJre

9

@android:style/Theme.NoTitleBar.Fullscreen 

或編程,我認爲你應該能夠做到這一點使用窗口標誌FEATURE_ACTION_BAR_OVERLAY:在這種情況下,你可以設置你的活動自定義主題在你的清單。

你叫你的活動setContentView()之前,

電話:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 

而且還會使用一個覆蓋ActionBar,而不是你的窗口倒推。

4

這是我如何得到這個工作:

1)請求動作條覆蓋程序通過調用

 getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 

這個 'requestFeature()' 必須從你的活動被要求「在調用'setContentView(R.layout.my_layout)'之前調用onCreate()'方法:

2) set通過調用setBackgroundDrawable()像這樣動作條顏色:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00212121"))); 

這種方法要求要麼可拉伸的引用或也可以用一個顏色解析器來解析一個十六進制顏色值(第一2個位數用於alpha通道開始在# 00至#FF)

請注意,我正在使用actionBarSherlok getSupportActionBar(),但是這應該適用於任何操作欄。

如果你仍然無法得到這個工作,你可以添加以下到您的主題風格

<item name="windowActionBarOverlay">true</item> 
<item name="android:actionBarStyle">@style/ActionBar.Transparent.Example</item> 
<item name="android:windowActionBarOverlay">true</item> 
5

上面的代碼是用於製作動作欄絕對正確的。

而不是

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0))) 

使用

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

這樣,你將能夠看到操作欄完全透明。

12

除了正確的答案,如果你正在使用AppcomatActivity,調用supportRequestWindowFeature代替

舊版本: getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

你想使用:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 
    super.onCreate(savedInstanceState); 
} 
+0

終於!謝謝你:) – Analizer

+0

謝謝!它爲我工作。 –