2011-12-11 37 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 –

相关问题