当宽度为match_parent时drawableRight接近文本

时间:2019-02-06 09:29:08

标签: android android-layout radio-button

我有一个radiobutton,右边带有文本和图片,我希望widthparent_width,但是drawableright接近页面上的文本对。我该如何实现? 我得到以下 enter image description here

我想在width中获得以下内容,但可以全屏点击 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>

2 个答案:

答案 0 :(得分:1)

您可以在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>

答案 1 :(得分:0)

  

好像您的RadioButton的父容器的宽度为“ match_parent

设置您的父容器宽度wrap_content”或用以下给定的代码段替换您的代码段

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioGroup
        android:layout_width="wrap_content"    //the only change is here
        android:layout_height="wrap_content"
        android:orientation="vertical">

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

    </RadioGroup>
</LinearLayout>