Android 将我的 ActionBar 放在底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12220428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:03:37  来源:igfitidea点击:

Placing my ActionBar at the bottom

androidxmlandroid-actionbar

提问by JoeyL

I followed a tutorial on getting actionbars in my app. But even when I put android:uiOptions="splitActionBarWhenNarrow"in my Manifest file it still keeps it at the top. Anyone has any ideas on what to do with this code?

我遵循了在我的应用程序中获取操作栏的教程。但即使我放入android:uiOptions="splitActionBarWhenNarrow"我的清单文件,它仍然保持在顶部。任何人都对如何处理此代码有任何想法?

XML:

XML:

<LinearLayout 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" 
android:orientation="vertical"
android:background="#f0f0f0" 
android:baselineAligned="false">

<LinearLayout 
    android:id="@+id/myFragments"
    android:layout_width="match_parent"
    android:layout_height="0dp">

</LinearLayout>

</LinearLayout>

Manifest File:

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alotoftesting"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" 
        android:uiOptions="splitActionBarWhenNarrow">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

采纳答案by JRaymond

The google default action bar does not in any case appear on the bottom. As it seems others have mentioned, splitActionBarWhenNarrowonly puts a subset of your ActionBar(like tabs, etc) on the bottom of the screen when the device is narrow. Unfortunately, if you want to implement an ActionBar like interface on the bottom of the screen, you'll have to implement it yourself (a quick search finds this example), as I haven't seen any Libraries to do this for you.

谷歌默认操作栏无论如何都不会出现在底部。正如其他人所提到的,当设备很窄时,splitActionBarWhenNarrow只会将您的一部分ActionBar(如标签等)放在屏幕底部。不幸的是,如果你想在屏幕底部实现一个类似 ActionBar 的界面,你必须自己实现它(快速搜索找到这个例子),因为我没有看到任何图书馆为你做这件事。

In all, though, I would recommend against it, as it violates the Seamlessness design principlein the design documents by breaking user expectations of where ActionBar type controls will be - An android user is ideally used to doing things a certain way, and you would need a pretty compelling reason to break that expectation.

不过,总而言之,我建议不要这样做,因为它违反了设计文档中的无缝设计原则,因为它打破了用户对 ActionBar 类型控件所在位置的期望 - android 用户理想地习惯于以某种方式做事,并且您会需要一个非常令人信服的理由来打破这种期望。

回答by piotrek1543

According to this comment:

根据此评论:

How many menu items do you have in your ActionBar? The splitActionBarWhenNarrow option basically allows overflow into a second, "split" action bar on the bottom if your menu items won't fit at the top. If all your menu items fit at the top you won't see the split layout.

您的 ActionBar 中有多少个菜单项?splitActionBarWhenNarrow 选项基本上允许溢出到底部的第二个“拆分”操作栏,如果您的菜单项不适合顶部。如果您的所有菜单项都位于顶部,您将看不到拆分布局。

If you would like to have a custom bottom toolbar, please check my answer to this question (added below):

如果您想要自定义底部工具栏,请查看我对这个问题的回答(在下面添加):

Creating custom bottom toolbar

I've already created a simple app which should demonstrate you how to begin

Creating a custom ViewGroup

Here's my activity_main.xmllayout file:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="0dp"
    tools:context="com.example.piotr.myapplication.MainActivity">

    <LinearLayout
        android:id="@+id/show_pdf"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@color/primary_material_light"
        android:orientation="horizontal">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_cut_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_copy_mtrl_am_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_paste_mtrl_am_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_share_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"/>
    </LinearLayout>

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"/>
</RelativeLayout>

As you can see my parent ViewGroupis RelativeLayout, which simply allows me to create a view at the bottom of screen.

Notice that I set layout padding to zero (I think: setting layout margin to zero here is not necessary, the same effect). If you'd change it, the toolbar won't use full width and it won't stick with bottom of the screen.

Then I added a Linear Layout with hardcoded height which is:

          android:layout_height="40dp"

I wanted it, that my bottom toolbar would take full available width so I set it as match_parent.

Next, I added some ImageButtonviews with images from Android library.

There you have two possibilities:

  • if you really want to have a toolbar like in above example just remove in every ImageButtonview this line:

          android:layout_weight="1"
    

After removing weights and some buttons you would get a view pretty similar to expected:

  • if you want to take the full width and make every button with the same size use in your project weightas in this mine example.

Now let's go to my AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="stateVisible|adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

In that file I'd added as you can see only one additional line:

         android:windowSoftInputMode="stateVisible|adjustResize">

to make sure that device keyboard won't hide my custom bottom toolbar.

From: How to add a bottom menu to Android activity

创建自定义底部工具栏

我已经创建了一个简单的应用程序,它应该向您展示如何开始

创建自定义视图组

这是我的activity_main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="0dp"
    tools:context="com.example.piotr.myapplication.MainActivity">

    <LinearLayout
        android:id="@+id/show_pdf"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@color/primary_material_light"
        android:orientation="horizontal">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_cut_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_copy_mtrl_am_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_paste_mtrl_am_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_share_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"/>
    </LinearLayout>

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"/>
</RelativeLayout>

正如你所看到的,我的父母ViewGroupRelativeLayout,它只是让我在屏幕底部创建一个视图。

请注意,我将 layout padding 设置为零(我认为:这里没有必要将 layout margin 设置为零,效果相同)。如果您更改它,工具栏将不会使用全宽,也不会粘在屏幕底部。

然后我添加了一个带有硬编码高度的线性布局,它是:

          android:layout_height="40dp"

我想要它,我的底部工具栏会占用全部可用宽度,所以我将它设置为match_parent.

接下来,我添加了一些ImageButton带有来自 Android 库的图像的视图。

你有两种可能:

  • 如果你真的想要一个像上面例子那样的工具栏,只需在每个ImageButton视图中删除这一行:

          android:layout_weight="1"
    

删除权重和一些按钮后,您将获得与预期非常相似的视图:

  • 如果你想在你的项目中使用全宽并使每个按钮的大小weight与这个我的例子中相同。

现在让我们去我的AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="stateVisible|adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

在我添加的那个文件中,你只能看到额外的一行:

         android:windowSoftInputMode="stateVisible|adjustResize">

确保设备键盘不会隐藏我的自定义底部工具栏。

来自:如何在 Android Activity 中添加底部菜单

I think in that way you can also put tabs at the bottom if needed.

我认为这样你也可以在需要时在底部放置标签。

If you have any question please free to ask.

如果您有任何问题,请随时提问。

Hope it help

希望有帮助