如何在 Framelayout 中的开始和中心位置之间放置一个按钮?

How to place a button between start and centre position inside Framelayout?

我在 Framelayout 中放置了 3 个按钮,每个按钮的布局重力如下:

  1. 开始|底部
  2. 中心|底部
  3. 结束|底部

现在我想在(开始和中心)和(结束和中心)之间放置两个额外的按钮。实现这一目标的最佳方法是什么?我无法在中心按钮的两侧使用 2 个水平线性布局,而且我不打算使用 BottomNavigationView。

或者您可以使用线性布局来实现这一点。像:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="5"
    android:orientation="horizontal"
    tools:context="com.myapplication.MainActivity">

    <Button
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:text="First" />

    <Button
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:text="two"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/third"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:text="third" />

    <Button
        android:id="@+id/fourth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:text="four" />

    <Button
        android:id="@+id/fifith"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:text="fifith" />

</LinearLayout>
why don't you use relative layout instead of frame layout. like this: 

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapplication.MainActivity">

   <Button
       android:id="@+id/first"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
      android:layout_alignParentStart="true"
       android:layout_alignParentLeft="true"
       android:layout_alignParentBottom="true"/>
    <Button
        android:id="@+id/second"
        android:layout_toRightOf="@+id/first"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/third"
        android:layout_toRightOf="@+id/second"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/fourth"
        android:layout_toRightOf="@+id/third"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/fifith"
        android:layout_toRightOf="@+id/fourth"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

您可以创建 2 个单独的线性布局,一个用于中心按钮的任一侧。左边应该有

<LinearLayout
  android:layout_gravity="start|bottom"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

--add 2 buttons

</LinearLayout>

而正确的应该是:

<LinearLayout
  android:layout_gravity="end|bottom"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
--add 2 buttons

</LinearLayout>

框架布局中的中心按钮应保持完整。