2011-12-11 42 views
42

冰淇淋三明治(Android 4.0)增加了將Action Bar置於手機屏幕底部的選項,這是我喜歡在我的應用程序中使用的選項。 docs提及uiOptions="splitActionBarWhenNarrow"用於當您需要某些內容時,即選項卡頂部和底部的Action Bar快捷方式。正如文檔中所描述的,我嘗試在應用程序清單中添加行,但到目前爲止還沒有工作。我如何強制行動欄在ICS的底部?

下面是一個例子:

enter image description here

另外,我注意到,在我的Galaxy Nexus,它運行ICS,該消息應用有Action Bar底部並沒有什麼,但在頂部的標題,所以它肯定有可能以某種方式迫使Action Bar處於底部。

任何想法?

+0

你可能在使用程序兼容性。 在ActionBarCompat中,通過在清單中將 'android.support.UI_OPTIONS'元數據字段設置爲'splitActionBarWhenNarrow'來完成此操作。 SAIR

回答

28

我已經嘗試在應用程序清單中添加行,如文檔中所述,但到目前爲止還沒有得到它的工作。

它適用於this sample project。這裏是清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest package="com.commonsware.android.actionbarbc" 
      xmlns:android="http://schemas.android.com/apk/res/android"> 

    <application android:hardwareAccelerated="true" 
       android:icon="@drawable/cw" 
       android:label="@string/app_name"> 
    <activity android:label="@string/app_name" 
       android:name=".InflationDemo" 
       android:uiOptions="splitActionBarWhenNarrow"> 
     <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    </application> 
    <uses-sdk android:minSdkVersion="4" 
      android:targetSdkVersion="11" /> 
    <supports-screens android:anyDensity="true" 
        android:largeScreens="true" 
        android:normalScreens="true" 
        android:smallScreens="true" 
        android:xlargeScreens="true" /> 
</manifest> 

另外,我注意到,在我的Galaxy Nexus,它運行ICS,該消息應用程序有操作欄的底部並沒有什麼,但在頂部的標題,所以它必須有可能以某種方式強制行動酒吧在底部。

如果你指的是會話列表,即ActionBar在頂部和底部,使用splitActionBarWhenNarrow和下面的設置代碼:

private void setupActionBar() { 
    ActionBar actionBar = getActionBar(); 

    ViewGroup v = (ViewGroup)LayoutInflater.from(this) 
     .inflate(R.layout.conversation_list_actionbar, null); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, 
      ActionBar.DISPLAY_SHOW_CUSTOM); 
    actionBar.setCustomView(v, 
      new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, 
        ActionBar.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER_VERTICAL | Gravity.RIGHT)); 

    mUnreadConvCount = (TextView)v.findViewById(R.id.unread_conv_count); 
} 
+0

謝謝,我來看看它。 –

+0

換句話說,解決方法是始終在頂部欄中有一個項目,以防止底部內容適合其中,從而迫使所有內容進入底部欄? – Matthias

+0

@Matthias:我沒有檢查過'R.layout.conversation_list_actionbar'的內容,對不起。 – CommonsWare

12

我使用ActionBarSherlock,我也有類似的問題。我通過將android:uiOptions="splitActionBarWhenNarrow"放在<activity>標記內而不是<application>標記中解決了問題。

例如,這工作:

<activity android:name=".my.Activity" 
      android:uiOptions="splitActionBarWhenNarrow"/> 

,這沒有奏效:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.slowchop.etc" 
      android:uiOptions="splitActionBarWhenNarrow"> 
+2

這幾乎肯定不能解決它,儘管我沒有很好的解釋,爲什麼這甚至會在所有方面帶來改變。您是否嘗試將您的應用切換到橫向模式?考慮到有足夠的空間,您的底部欄可能會出現在頂部。一個簡單的例子就是ActionBarSherlock示例應用程序。它在活動中聲明瞭'splitActionBarWhenNarrow',但只有在肖像模式下它纔會顯示在屏幕的底部。 – Matthias

3

以及你不能強迫留在底部在平板電腦上,但如果手機是啊,你可以通過清單來做到這一點。但你可以做一些類似於底部欄和頂部欄的東西。 在這個例子中,我會告訴你如何使用merge來輕鬆完成,而不需要使用android的ActionBar。

您需要創建的第一件事是您的main_activity.xml在我的情況下,main_activity.xml僅在RelativeLayout上包含ImageView。這裏是代碼。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 
     <RelativeLayout android:id="@+id/RelativeLayout04" 
    android:layout_width="match_parent" android:layout_height="wrap_content" 
    android:layout_alignParentTop="true"> 

    <include layout="@layout/header" /> 

</RelativeLayout> 

<ImageView 
    android:id="@+id/view" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:layout_above="@+id/RelativeLayout03" 
    android:layout_below="@+id/RelativeLayout04" 
    android:layout_centerHorizontal="true" 
    android:src="@android:drawable/alert_dark_frame" /> 

    <RelativeLayout android:id="@+id/RelativeLayout03" 
    android:layout_width="match_parent" android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"> 

    <include layout="@layout/tryit" /> 

</RelativeLayout> 

,你可以看到在上面的代碼中,有兩個合併我把裏面的main_activity.xml一個在底部的定義,一個在頂部定義。 這裏是假底部酒吧xml。

<merge xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout 
    android:id="@+id/LinearLayout01" 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    android:layout_weight="0.14" 
    android:background="@drawable/dock" > 

    <ImageView 
     android:id="@+id/dark" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.14" /> 

    <ImageView 
     android:id="@+id/stock" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.14" /> 

    <ImageView 
     android:id="@+id/open" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.14" /> 

    <ImageView 
     android:id="@+id/border" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.15" /> 

    <ImageView 
     android:id="@+id/color" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.15" 
     /> 

</LinearLayout> 

我把一個固定的背景,LinearLayout和僞造的ImageView爲onClicks。

這裏是頂尖的酒吧。 `

<LinearLayout 
    android:id="@+id/LinearLayout02" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:layout_weight="0.14" 
    android:background="@drawable/dock1" 
    android:layout_gravity="top"> 

    <ImageView 
     android:id="@+id/darka" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.14" /> 

    <ImageView 
     android:id="@+id/stocka" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.14" /> 

    <ImageView 
     android:id="@+id/opena" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.14" /> 

    <ImageView 
     android:id="@+id/bordera" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.15" /> 

    <ImageView 
     android:id="@+id/colora" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.15" 
     /> 

</LinearLayout> 

'

其也從上述底部欄複製粘貼。只需將android:layout_alignParentBottom="true"中的一項更改爲android:layout_alignParentTop="true"即可,並且您在底部和頂部都有一個actionBar。在這種情況下,你不會需要使用動作條,所以我建議你使用Theme.Holo.NoActionBar

這裏是圖像結果: - http://i.imgur.com/N8uKg6v.png

這是一個項目,我的工作現在。完成了幾乎所有的事情,但仍然與設計掙扎。希望我的回答能讓你受益。如果您覺得有趣,請回答問題。
此致敬意。 〜Kosh

4

編輯這裏是圖像:)。 InstaGram ActionBar + Bottom Bar

回來了,最好的結果:)。你需要做的第一件事就是 創建佈局命名header.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="@dimen/action_bar_height" 
    android:layout_gravity="top" 
    android:baselineAligned="true" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/share" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" 
     android:layout_weight=".14" 
     android:background="@drawable/action_bar_left_button" 
     android:src="@drawable/action_bar_glyph_back" /> 

    <ImageView 
     android:id="@+id/bright" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".14" 
     android:background="@drawable/action_bar_left_button" 
     android:src="@drawable/action_bar_glyph_lux" /> 

    <ImageView 
     android:id="@+id/rotate" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".14" 
     android:background="@drawable/action_bar_left_button" 
     android:src="@drawable/rotate" /> 

    <ImageView 
     android:id="@+id/bright" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".14" 
     android:background="@drawable/action_bar_left_button" 
     android:src="@drawable/action_bar_glyph_lux" /> 

    <ImageView 
     android:id="@+id/rotate" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".14" 
     android:background="@drawable/action_bar_left_button" 
     android:src="@drawable/rotate" /> 

    <ImageView 
     android:id="@+id/forwa" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".14" 
     android:background="@drawable/action_bar_left_button" 
     android:src="@drawable/forward" /> 

</LinearLayout> 

後去你MainActivity.class創建此方法。

private void setupActionBar() { 
    ActionBar actionBar = getActionBar(); 
    //actionBar.setDisplayShowHomeEnabled(false); 
    actionBar.setDisplayShowTitleEnabled(false); 
    ViewGroup v = (ViewGroup)LayoutInflater.from(this) 
     .inflate(R.layout.header, null); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, 
      ActionBar.DISPLAY_SHOW_CUSTOM); 
    actionBar.setCustomView(v, 
      new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, 
        ActionBar.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER_VERTICAL | Gravity.RIGHT)); 

} 

setupActionBar();添加到您的onCreate活動並運行您的應用程序:)。

現在你有自定義的帶有分隔符和圖像的ActionBar P.S分隔符在佈局中被定義爲圖像背景:)。

+0

首先,非常感謝這真是幫了我,除了我面臨的一個問題,即操作欄佈局犯規填滿屏幕的寬度。 –

0

試試這個...

private View contentView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    contentView = getLayoutInflater().inflate(R.layout.activity_main, null); 
    setContentView(contentView); 
    LinearLayout layout = (LinearLayout) contentView.getParent().getParent(); 
    View view = layout.getChildAt(0); 
    layout.removeViewAt(0); 
    layout.addView(view); 
} 
+0

是否在發佈之前嘗試過?這沒有工作:) –

+0

是的。我曾嘗試在 –

+0

正在使用真實的設備有4.2,而其沒有工作的Nexus 4.1模擬器:d –

相關問題