【问题标题】:Increasing size of clickable area of RadioButton增加 RadioButton 的可点击区域的大小
【发布时间】:2018-12-20 14:01:05
【问题描述】:

我在 Android (Android 5.1) 中有一个 RadioGroup 和 2 个 RadioButton,如下所示。有两个问题。首先,可点击区域(在底部的图像中以蓝色描绘)不是以按钮(圆圈)为中心。其次,我想增加可点击区域但保持圆圈(按钮)大小相同。

如何增加按钮周围的可点击区域并使其居中?

<RadioGroup
    android:id="@+id/group"
    android:layout_width="140dp"
    android:layout_height="50dp"
    android:layout_marginLeft="212dp"
    android:layout_marginTop="8dp"
    android:orientation="horizontal"
    android:visibility="visible"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/image"
    tools:ignore="RtlHardcoded">

    <RadioButton
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:scaleX="2"
        android:scaleY="2" />

    <RadioButton
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:scaleX="2"
        android:scaleY="2" />

</RadioGroup>

编辑:我现在更新了布局并合并了填充。可点击区域没有更大,但不幸的是该区域在按钮之外。我想让可点击区域以按钮为中心。如何做到这一点?

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="212dp"
    android:layout_marginTop="8dp"
    android:orientation="horizontal"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/image"
    tools:ignore="RtlHardcoded">

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:padding="30dp"
        android:scaleX="2"
        android:scaleY="2" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="55dp"
        android:padding="30dp"
        android:scaleX="2"
        android:scaleY="2" />

</RadioGroup>

【问题讨论】:

    标签: android radio-button radio-group android-gui


    【解决方案1】:

    final View parent = (View) view.getParent();
    parent.post( new Runnable() {
        public void run() { 
            final Rect rect = new Rect(); 
            button.getHitRect(rect); 
            rect.top -= 100;   
            rect.left -= 100;   
            rect.bottom += 100;          
            rect.right += 100;  
            parent.setTouchDelegate( new TouchDelegate( rect , button)); 
        } 
    }); 

    增加视图的删除

    【讨论】:

    • 我已经尝试过了,但不幸的是它根本不会移动单选按钮的点击矩形。我不知道为什么。
    【解决方案2】:

    可能在线性布局中添加单选按钮,将其居中,然后将 onclicklistener 放在线性布局上,然后在单击时切换单选按钮的视觉效果。

    【讨论】:

      【解决方案3】:

      增加可点击大小的最简单方法是使用填充

      <RadioButton
              android:id="@+id/someId"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="40dp"
              />
      

      这是图片

      【讨论】:

      • 我添加了内边距,但按钮现在位于可点击区域之外。请在问题中查看我的编辑。
      • 移除 android:layout_marginLeft
      【解决方案4】:

      要增加可点击区域的大小,您可以将 padding 设置为 RadioButton。

      【讨论】:

      • 我添加了填充,但按钮现在位于可点击区域之外。请在问题中查看我的编辑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多