【问题标题】:Android Radio Group alignment over buttons in relative layoutAndroid Radio Group在相对布局中的按钮上对齐
【发布时间】:2023-03-05 22:52:01
【问题描述】:

我无法将我的广播组定位在按钮上。它以相对布局返回按钮。我想要的是一个按钮上的广播组。我已经添加了代码的输出。我想要的是把收音机组放在 2 个按钮上。谁能帮我解决这个 UI 问题?

<?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"
android:focusable="true"
tools:context=".MainActivity">

<Button
    android:id="@+id/buttonBelow"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:background="#f00"
    android:text="Button Below" />

<Button
    android:id="@+id/buttonAbove"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:background="#ff0"
    android:text="Button Above" />


<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#00f">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />

</RadioGroup>


</RelativeLayout>

【问题讨论】:

  • 添加应用的当前屏幕截图以进行可视化。
  • 如果您发布当前输出和预期输出的图像会很好。
  • @Zlytherin 当前屏幕截图与应用程序中的相同。我希望这两个按钮都回到前面的收音机组和收音机组。
  • @RumitPatel 我想要收音机组在前面,两个按钮在后面
  • @AmitMishra,检查我的答案。

标签: android user-interface android-relativelayout android-radiogroup


【解决方案1】:

更改您的 xml:

<?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"
    android:focusable="true">


<FrameLayout android:id="@+id/lin" android:layout_width="match_parent" android:layout_height="wrap_content"
             android:orientation="vertical" android:layout_centerInParent="true" android:layout_marginTop="24dp">



    <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center">

        <Button
                android:id="@+id/buttonBelow"
                android:layout_width="300dp"
                android:layout_gravity="center"
                android:layout_height="300dp"
                android:background="#f00"
                android:text="Button Below"/>

        <Button
                android:id="@+id/buttonAbove"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_gravity="center"
                android:background="#ff0"
                android:text="Button Above"/>

    </FrameLayout>

    <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#00f">

        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Male"/>

        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"/>

    </RadioGroup>


</FrameLayout>

</RelativeLayout>

【讨论】:

  • 很好尝试@Piyush,但输出不是所需的输出。我想要按钮上的单选组,这意味着两个按钮都返回单选组和单选组超过 2 个按钮。
  • @AmitMishra,不是每次都解释,而是发布预期输出和当前输出的图像。
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <Button
            android:id="@+id/buttonBelow"
            android:layout_width="300dp"
            android:layout_height="300dp"

            android:background="#f00"
            android:text="Button Below"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <Button
            android:id="@+id/buttonAbove"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="#ff0"
            android:text="Button Above"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00f">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Male"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"/>

        </RadioGroup>
    </LinearLayout>

</RelativeLayout>

【讨论】:

    【解决方案3】:
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
    
        <Button
            android:id="@+id/buttonBelow"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_centerInParent="true"
            android:background="#f00"
            android:text="Button Below" />
    
        <Button
            android:id="@+id/buttonAbove"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:background="#ff0"
            android:text="Button Above" />
    </RelativeLayout>
    
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#00f">
    
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Male" />
    
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female" />
    
    </RadioGroup>
    

    【讨论】:

      【解决方案4】:

      this answer, 我们只需将RadioGroup 提升1dp. 没有其他更改。

      这是因为Button 有默认的android:elevation="1dp". 所以对于RadioGroup, 我们需要设置更多的高度。

      <Button
          android:id="@+id/buttonBelow"
          android:layout_width="300dp"
          android:layout_height="300dp"
          android:layout_centerInParent="true"
          android:background="#f00"
          android:text="Button Below" />
      
      <Button
          android:id="@+id/buttonAbove"
          android:layout_width="200dp"
          android:layout_height="200dp"
          android:layout_centerInParent="true"
          android:background="#ff0"
          android:text="Button Above" />
      
      
      <RadioGroup
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:background="#00f"
          android:elevation="2dp">
      
          <RadioButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:checked="true"
              android:text="Male" />
      
          <RadioButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Female" />
      
      </RadioGroup>
      

      【讨论】:

        【解决方案5】:

        试试这个

        <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"
        android:focusable="true"
        tools:context=".MainActivity">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <Button
                android:id="@+id/buttonBelow"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:layout_centerInParent="true"
                android:background="#f00"
                android:text="Button Below" />
        
            <Button
                android:id="@+id/buttonAbove"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_centerInParent="true"
                android:background="#ff0"
                android:text="Button Above" />
        </RelativeLayout>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="#00f">
        
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Male" />
        
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female" />
        </RadioGroup>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多