【问题标题】:How to set layouts instead of text in RadioButton如何在 RadioButton 中设置布局而不是文本
【发布时间】:2020-09-17 04:58:15
【问题描述】:

通常单选按钮,你可以有文字,有没有可能添加一个布局而不是如下图所示的文字?

附注

我将所有单选按钮保存在一个单选组中。只是为了这个布局,我不想沿着无线电组创建一个单独的线性布局。

【问题讨论】:

    标签: android android-layout radio-button android-radiobutton


    【解决方案1】:

    这个答案可能有点矫枉过正,但你可以创建一个自定义视图,我将你所拥有的与以下内容放在一起

    custom_radio_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        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="wrap_content"
        tools:context=".MainActivity">
    
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:text="For one hour"
            app:layout_constraintBottom_toBottomOf="@+id/radioButton"
            app:layout_constraintStart_toEndOf="@+id/radioButton"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:text="Until 9:08pm"
            app:layout_constraintBottom_toBottomOf="@+id/radioButton"
            app:layout_constraintStart_toEndOf="@+id/radioButton"
            app:layout_constraintTop_toBottomOf="@+id/textView"
            app:layout_constraintVertical_bias="0.0" />
    
        <Button
            android:id="@+id/button"
            style="@style/Widget.AppCompat.Light.ActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:textColor="@android:color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            style="@style/Widget.AppCompat.Light.ActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="12dp"
            android:text="-"
            android:textColor="@android:color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button"
            app:layout_constraintTop_toTopOf="parent" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    RadioCustomLayout.kt

    class RadioCustomLayout: ConstraintLayout {
        constructor(context: Context?): super(context)
        constructor(context: Context?, attrs: AttributeSet?): super(context, attrs)
        constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
    
        init {
            inflate(context, R.layout.custom_radio_view, this)
        }
    
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <com.test.test.RadioCustomLayout
            android:id="@+id/group1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    通过一些 declare-styleable 和回调,您可以创建一个完全可重复使用的布局,而不是一个无线电组

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 2015-08-09
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多