android - drawableRight 靠近文本时宽度匹配_parent

标签 android android-layout radio-button

我有一个 radiobutton,右侧有文字和图片,我希望 widthparent_width,但是 drawableright 靠近右边的文字。我怎样才能做到这一点? 我得到以下 enter image description here

我想获得以下但全屏可点击的宽度 enter image description here

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/icon"
            tools:text="Test"/>

    </RadioGroup>
</LinearLayout>

最佳答案

您可以对 RadioButton 应用结束(右)填充,这会将可绘制对象向左移动。问题是应该应用多少填充?由于填充量会根据多种因素而变化,因此必须在代码中进行计算。 (如果您有一个明确定义的环境并且知道文本长度不会改变,您可以只在 XML 中设置填充。)

enter image description here

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private RadioButton mRadioButton;
    private RadioButton mRadioButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRadioButton = findViewById(R.id.radioButton);
        mRadioButton2 = findViewById(R.id.radioButton2);

        RadioGroup rg = findViewById(R.id.radioGroup);
        rg.post(new Runnable() {

            @Override
            public void run() {
                moveDrawable(mRadioButton);
                moveDrawable(mRadioButton2);
            }

            private void moveDrawable(RadioButton radioButton) {
                // Get the drawables for this radio button. If the right drawable is not null
                // then we need to move it to the end of the text.
                Drawable[] drawables = radioButton.getCompoundDrawables();
                if (drawables[2] != null) {
                    // Compute how much end padding to apply to get the drawable beside the text.
                    int textLen = (int) radioButton.getPaint().measureText((String) radioButton.getText());
                    // 4dp between the text and the drawable just to separate them a little.
                    int drawableLeftPadding =
                        (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                                        4, getResources().getDisplayMetrics());
                    int paddingEnd = radioButton.getWidth() -
                        (radioButton.getCompoundPaddingLeft() + +textLen + drawables[2].getIntrinsicWidth())
                        - drawableLeftPadding;
                    radioButton.setPadding(0, 0, paddingEnd, 0);
                }
            }
        });
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    android:orientation="vertical"
    android:padding="16dp">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light"
            android:drawableEnd="@drawable/ic_insert_emoticon_yellow_24dp"
            android:text="Test Test Test Test Test " />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_light"
            android:drawableEnd="@drawable/ic_insert_emoticon_yellow_24dp"
            android:text="Test" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light"
            android:drawableEnd="@drawable/ic_insert_emoticon_yellow_24dp"
            android:text="Test" />

    </RadioGroup>

</LinearLayout>

关于android - drawableRight 靠近文本时宽度匹配_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54550292/

相关文章:

php - Android 设备向 PHP 发送空数据

android - 清除多行 EditText

android - 我无法在 res 目录中添加新文件夹

android - 将可绘制对象添加到 CirclePageIndicator

html - css 或 bootstrap 中是否有任何功能可以增加 IE 8 中单选按钮的可见性?

android - 如何从发布的 APK 中排除库 JAR 中包含的非类文件?

android - '找不到 Activity 的兼容 AVD 或设备

android - 如何使用 LayoutInflater/ViewStub 进行叠加

javascript - 单击单选按钮时选中复选框

javascript - 如何在文本更改/添加时保持单选按钮静态