【问题标题】:How to enable Split Action Bar?如何启用拆分操作栏?
【发布时间】:2015-12-31 11:53:57
【问题描述】:

我想创建一个 android 应用程序,它有 3 个滑动标签面板,每个都有 5 个按钮(保存、新建、删除、退出..)。

我想要的完全如下:

我创建了滑动选项卡面板。对于 5 个按钮,我添加了拆分操作栏。但它可以作为正常的拆分操作栏工作。我的 AndroidManifest.xml 是:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:uiOptions="splitActionBarWhenNarrow"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

我哪里错了?

【问题讨论】:

  • Theme.MaterialTheme.AppCompat 不支持拆分操作栏模式。您需要创建自己的 Toolbar 包含“拆分”项目并将其放置在底部的活动中。

标签: android android-actionbar splitactionbar


【解决方案1】:

实现splitActionBar

只需将android:uiOptions="splitActionBarWhenNarrow" 添加到AndroidManifest.xml 中的activity 标记,就像这样...

`<activity
    android:name=".MainActivity"
    android:uiOptions="splitActionBarWhenNarrow">`<br>

你可以阅读更多herehere

注意:它仅适用于具有屏幕宽度的手机设备 400dp.

创建自定义底部工具栏:

如果您想为所有设备设置它,请在此处查看我的答案(查找以Creating custom bottom toolbar 开头的帖子):

创建自定义底部工具栏

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

创建自定义视图组

这是我的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,这很简单 允许我在屏幕底部创建一个视图。

请注意,我将布局填充设置为零(我认为:设置布局 此处的保证金为零是没有必要的,效果相同)。如果你愿意 改变它,工具栏不会使用全宽,也不会坚持 屏幕底部。

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

          android:layout_height="40dp"

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

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

你有两种可能:

  • 如果你真的想要像上面的例子那样有一个工具栏,只需删除每个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">

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

发件人:How to add a bottom menu to Android activity

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

希望对你有帮助

【讨论】:

  • 感谢您的详细评论。但我不明白为什么在聚焦edittext时工具栏会改变位置。当键盘出现时,工具栏会改变位置到键盘顶部。如何克服这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多