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

如何为Android活动添加底部菜单,android,android-layout,menu,android-actionbar,Android,Android Layout,Menu,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" and

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

大概是这样的:

以下是我的布局:

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


我尝试添加一个自定义RelativeLayout,它看起来几乎像一个操作栏,但工具栏会弹出,把一切都搞糟。

这是一个拆分的操作栏。它适用于屏幕宽度为400dp<的手持设备要启用拆分操作栏,只需将uiOptions=“splitActionBarWhenNarrow”添加到或manifest元素中即可

编辑:第二个图像实际上不是拆分操作栏。这些只是具有buttonBarButtonStyle属性的按钮。请在此处查看。

创建自定义底部工具栏 我已经创建了一个简单的应用程序,可以演示如何开始

创建自定义视图组 这是我的
activity\u main.xml
layout文件:

<?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>
我想要它,我的底部工具栏将采用全部可用宽度,因此我将其设置为
match\u parent

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

这里有两种可能性:

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

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

  • 如果你想获得全宽,并使每个按钮在你的项目中使用相同的大小
    权重
    ,就像我的例子一样
现在让我们转到我的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>
确保设备键盘不会隐藏自定义底部工具栏

如果您有任何问题,请随时提问。我会尽快回答


希望对您有所帮助

我制作了一个自定义底部栏,精确高度为?attr/actionBarSize,并使用“layout\u alignParentBottom=“true”将其放在底部

<LinearLayout
    android:id="@+id/bottombar"
    android:layout_width="fill_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentBottom="true"
    android:background="#FFFFFF"
    android:weightSum="30"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:src="@drawable/iconcamera"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:src="@drawable/shape"/>
</LinearLayout>`

这将调整键盘开口上的布局。

您只需添加工具栏并将其设置为底部,即可创建自己的自定义布局

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="#333333" app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_alignParentBottom="true"

        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="left">

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/folder"
                android:id="@+id/pdfViewer"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            >
            <Button
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:background="@drawable/cameranew"
                android:id="@+id/btn"
                />
        </LinearLayout>
       </android.support.v7.widget.Toolbar> 


检查是否有帮助此处没有提供链接@yasser如果此选项被激活,Android可以选择拆分操作栏。是否拆分由系统在运行时决定。您可以定义操作栏应由系统自动拆分。如果没有足够的可用空间,您可以通过android:uiOptions=“SplitActionBarWhenNarrow”激活此操作栏参数在AndroidManifest.xml文件中声明您的应用程序活动。现在,您可以将工具栏放在底部,然后在那里设置操作栏。请参考此链接hi piotrek,它工作正常,但我想在这里隐藏状态栏,是否可能?@user512很高兴听到这个消息。检查此项:感谢piotrek的回复。我试过上面的代码,它隐藏了状态栏,但当键盘出现时,并没有显示键盘上方的工具栏。@user512在StackOverflow上创建一个新问题
<LinearLayout
    android:id="@+id/bottombar"
    android:layout_width="fill_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentBottom="true"
    android:background="#FFFFFF"
    android:weightSum="30"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:src="@drawable/iconcamera"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:src="@drawable/shape"/>
</LinearLayout>`
android:windowSoftInputMode="adjustResize"
 <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="#333333" app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_alignParentBottom="true"

        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="left">

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/folder"
                android:id="@+id/pdfViewer"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            >
            <Button
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:background="@drawable/cameranew"
                android:id="@+id/btn"
                />
        </LinearLayout>
       </android.support.v7.widget.Toolbar>