【问题标题】:How to add a bottom menu to Android activity如何将底部菜单添加到 Android 活动
【发布时间】:2016-03-22 13:32:53
【问题描述】:

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

类似这样的:

这是我的布局:

<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,它看起来几乎像一个操作栏,但工具栏会升起并弄乱一切。

【问题讨论】:

  • 检查this是否有帮助

标签: android android-layout menu android-actionbar


【解决方案1】:

创建自定义底部工具栏

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

创建自定义视图组

这是我的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>

如您所见,我的父 ViewGroup 是 RelativeLayout,这让我可以在屏幕底部创建一个视图。

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

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

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

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

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

希望对你有帮助

【讨论】:

  • 现在您可以将工具栏放在底部,然后在此处设置操作栏,请参阅此链接stackoverflow.com/questions/26660515/…
  • 嗨,piotrek,它工作得很好,但我也想在这里隐藏状态栏,可以吗?
  • @user512 很高兴听到。检查这个:developer.android.com/training/system-ui/status.html
  • 感谢 piotrek 的回复。我试过上面的代码,当键盘出现时它隐藏了状态栏但没有在键盘上方显示工具栏。
  • @user512 在 StackOverflow 上创建一个新问题
【解决方案2】:

我制作了一个自定义底部栏,其高度为 ?attr/actionBarSize 和 `layout_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>`

现在转到 Manifest.xml 并在您的活动标签中放置

android:windowSoftInputMode="adjustResize"

这将调整键盘打开时的布局。

【讨论】:

    【解决方案3】:

    那是一个拆分操作栏。它适用于屏幕宽度为 400dp 的手机设备

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

    【讨论】:

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

    您可以简单地添加一个工具栏并将其设置为底部,并且可以创建自己的自定义布局。

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

    【讨论】:

      猜你喜欢
      • 2014-04-28
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 2017-11-18
      • 1970-01-01
      相关资源
      最近更新 更多