如何向Android活动添加底部菜单

Str*_* B. 12 android menu android-layout android-actionbar

我正在尝试在活动布局的底部添加另一个操作栏或菜单,并保留顶部操作栏.

像这样的东西:

我的布局

这是我的布局:

<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"
tools:context="com.example.yasser.version6.PublierActivity">


<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />



<RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="match_parent"
     >


    <LinearLayout
        android:layout_width="fill_parent"
        android:id="@+id/user"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">


        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_margin="4dp"
            android:id="@+id/profil_image"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="218dp"
            android:layout_marginEnd="218dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:ems="10"
            android:background="#00000000"
            android:layout_margin="4dp"
            android:hint="Écriver quelque chose..."
            android:id="@+id/publication_text"
            android:maxLength="150"
            android:textSize="15dp"
            android:maxLines="3"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
            <requestFocus />
        </EditText>


    </LinearLayout>




    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="@dimen/image_size"
        android:id="@+id/publication_image"
        android:layout_weight="1"
        android:padding="0dp"
        android:background="@drawable/default_image"
        android:layout_marginTop="5dp"
        android:scaleType="fitXY"
        android:layout_below="@+id/user" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我试图添加一个自定义RelativeLayout,看起来几乎像一个操作栏,但工具栏上升,搞砸了一切.

pio*_*543 16

创建自定义底部工具栏

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

创建自定义ViewGroup

这是我的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>
Run Code Online (Sandbox Code Playgroud)

正如你可以看到我的父母ViewGroupRelativeLayout,它只是允许我创建在屏幕底部的视图.

请注意,我将布局填充设置为零(我认为:此处将布局边距设置为零是不必要的,效果相同).如果您更改它,工具栏将不会使用全宽,它不会粘在屏幕的底部.

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

          android:layout_height="40dp"
Run Code Online (Sandbox Code Playgroud)

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

接下来,我ImageButton使用Android库中的图像添加了一些视图.

那里有两种可能性:

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

  • 如果你想获取整个宽度并使你的项目中使用相同大小的每个按钮,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>
Run Code Online (Sandbox Code Playgroud)

在我添加的文件中,您只能看到一行:

         android:windowSoftInputMode="stateVisible|adjustResize">
Run Code Online (Sandbox Code Playgroud)

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

如果您有任何疑问,请随意提问.我会尽快回答他们.

希望它有所帮助


Er.*_*pat 0

那是一个分割操作栏。它适用于屏幕宽度为 400dp< 的手机设备。要启用拆分操作栏,只需将 uiOptions="splitActionBarWhenNarrow" 添加到您的或清单元素即可。

编辑:第二张图片确实不是分割操作栏。这些只是带有buttonBarButtonStyle 属性的按钮。在这里看看这个。

  • “这里”没有提供链接 (4认同)
  • @yasser 如果激活此选项,Android 可以选择拆分操作栏。是否分割由系统在运行时决定您可以定义如果没有足够的可用空间,操作栏应由系统自动分割您可以通过应用程序声明中的 android:uiOptions="SplitActionBarWhenNarrow" 参数激活它AndroidManifest.xml 文件中的活动。 (2认同)