【问题标题】:How can I force the Action Bar to be at the bottom in ICS?如何强制操作栏位于 ICS 的底部?
【发布时间】:2013-11-28 18:48:25
【问题描述】:

Ice Cream Sandwich (Android 4.0) 添加了在手机屏幕底部显示Action Bar 的选项,这是我希望在我的应用程序中拥有的功能。 docs 提到 uiOptions="splitActionBarWhenNarrow" 用于当您想要某些东西时,即顶部的选项卡和底部的 Action Bar 快捷方式。如文档中所述,我已尝试在应用程序清单中添加该行,但到目前为止还没有工作。

这是一个例子:

另外,我注意到在运行 ICS 的 Galaxy Nexus 上,消息传递应用程序的底部是 Action Bar,顶部只有标题,所以必须有可能以某种方式强制 Action Bar在底部。

有什么想法吗?

【问题讨论】:

  • 您可能正在使用 appCompat。在 ActionBarCompat 中,这是通过在清单中将“android.support.UI_OPTIONS”元数据字段设置为“splitActionBarWhenNarrow”来完成的。

标签: android android-actionbar android-4.0-ice-cream-sandwich


【解决方案1】:

我已尝试在应用程序清单中添加该行,如文档中所述,但到目前为止还没有工作。

它在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>

另外,我注意到在运行 ICS 的 Galaxy Nexus 上,消息传递应用程序的底部有操作栏,顶部只有标题,所以必须有可能以某种方式强制操作栏位于底部。

如果您指的是对话列表,即顶部和底部的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);
}

【讨论】:

  • 谢谢,我去看看。
  • 换句话说,解决方法是在顶部栏中始终有一个项目,以防止底部内容永远适合那里,从而迫使所有内容进入底部栏?
  • @Matthias:我还没有检查R.layout.conversation_list_actionbar的内容,抱歉。
  • @CommonsWare 嗨,我不知道如何以编程方式添加这些内容。您可以在这里查看我的问题http://stackoverflow.com/questions/23821414/move-the-action-bar-tab-to-the-bottom-of-the-screen。如果您知道如何解决这些问题,请帮助我。谢谢跨度>
【解决方案2】:

我正在使用 ActionBarSherlock,但遇到了类似的问题。我通过将android:uiOptions="splitActionBarWhenNarrow" 放在&lt;activity&gt; 标签而不是&lt;application&gt; 标签中解决了这个问题。

例如,这有效:

<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">

【讨论】:

  • 这几乎肯定不能解决问题,尽管我无法很好地解释为什么这甚至会产生影响。您是否尝试过将应用切换到横向模式?鉴于有足够的空间,您的底部栏现在可能会出现在顶部。一个简单的示例是 ActionBarSherlock 示例应用程序。它在活动上声明splitActionBarWhenNarrow,但只有在纵向模式下它才会实际显示在屏幕底部。
【解决方案3】:

编辑这里是图像:)。

再次以最佳结果返回 :)。你需要做的第一件事是 创建一个布局,命名为 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 Activity 并运行您的应用程序:)。

现在您有了带有分隔线和图像的自定义 ActionBar P.S 分隔线在布局中定义为图像背景:)。

【讨论】:

  • 首先,非常感谢这对我有很大帮助,但我遇到了操作栏布局无法填满屏幕宽度的问题。
【解决方案4】:

好吧,你不能强迫在平板电脑上停留在底部,但如果是手机,你可以通过清单来做到这一点。但你可以做一些类似于底栏和顶栏的事情。 在本例中,我将向您展示如何使用合并轻松完成此操作,而无需使用 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 设置了一个固定的背景,并为onClicks 伪造了ImageView。

这里是顶部栏。 `

<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。在这种情况下你不需要使用 ActionBar 所以我建议你使用Theme.Holo.NoActionBar

这是图像结果:- http://i.imgur.com/N8uKg6v.png

这是我现在正在做的一个项目。几乎完成了所有工作,但仍在努力设计。希望我的回答对你有所帮助。如果您觉得有趣,请投票给答案。
此致。 〜科什

【讨论】:

    【解决方案5】:

    试试这个...

    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);
    }
    

    【讨论】:

    • 发帖前你试过了吗?这不起作用:)
    • 我正在使用带有 4.2 的 Real Device,但它无法正常工作:D
    • 我试试然后回复你
    猜你喜欢
    • 2021-04-15
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    相关资源
    最近更新 更多