gpt4 book ai didi

android - 如何向 Android Activity 添加底部菜单

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:36:05 24 4
gpt4 key购买 nike

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

像这样:

My Layout

这是我的布局:

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

最佳答案

创建自定义底部工具栏

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

创建自定义 View 组

这是我的 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,它只允许我在屏幕底部创建一个 View 。

注意我把layout padding设置为0(我觉得:这里设置layout margin为0不是必须的,一样的效果)。如果您更改它,工具栏将不会使用全宽,也不会固定在屏幕底部。

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

          android:layout_height="40dp"

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

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

你有两种可能性:

  • 如果您真的想要像上面示例中那样的工具栏,只需在每个 ImageButton View 中删除此行:

          android:layout_weight="1"

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

  • 如果你想采用全宽并使每个按钮的大小都相同,请在你的项目中使用 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">

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

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

希望对你有帮助

关于android - 如何向 Android Activity 添加底部菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34311601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com