如何在 Android Activity 中添加底部菜单 [英] How to add a bottom menu to Android activity

查看:32
本文介绍了如何在 Android Activity 中添加底部菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add another action bar or menu to the bottom of my activity layout and keep the top action bar.

像这样:

这是我的布局:

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

我尝试添加一个自定义的相对布局,它看起来几乎像一个操作栏,但工具栏上升并弄乱了所有内容.

I've tried to add a custom RelativeLayout which look almost like an action bar, but the toolbar goes up and mess up everything.

推荐答案

创建自定义底部工具栏

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

Creating custom bottom toolbar

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

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

Here's my activity_main.xml layout 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>

正如你所看到的,我的父 ViewGroupRelativeLayout,它允许我在屏幕底部创建一个视图.

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

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

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"

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

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

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

Next, I added some ImageButton views with images from Android library.

你有两种可能:

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

      android:layout_weight="1"

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

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

  • 如果你想在你的项目中使用全宽并使每个按钮具有相同的大小,请使用 weight 就像在这个我的例子中一样.
  • if you want to take the full width and make every button with the same size use in your project weight as in this mine example.

现在让我们转到我的 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.

如果您有任何问题,请随时提问.我会尽快回复他们.

If you have any question, please free to ask. I would answer them as quickly as possible.

希望对你有帮助

这篇关于如何在 Android Activity 中添加底部菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆