【问题标题】:drawableRight close to the text when width match_parent宽度 match_parent 时 drawableRight 靠近文本
【发布时间】:2019-06-30 05:01:37
【问题描述】:

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

我想在width 中获得以下但可全屏点击的内容

<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>

【问题讨论】:

  • 能否请您在此处粘贴您的布局,这些信息还不够...
  • @MahendraGohil 已粘贴

标签: android android-layout radio-button


【解决方案1】:

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

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>

【讨论】:

  • 谢谢!虽然我想要一个只使用 xml 的解决方案,但它也可以
  • @Nikitc 没有我知道的纯 XML 解决方案,除非您想将自己限制在可以以文本方式表示的事物上,例如表情符号。 Test😀 会在最后给你带笑脸的文字。
【解决方案2】:

似乎您的 RadioButton 的父容器的宽度为“match_parent

使您的父容器宽度wrap_content”或将您的 sn-p 替换为给定的如下

<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>

【讨论】:

  • 不,我只想要文本旁边的箭头,但单选按钮本身就是全宽。谢谢
  • 立即查看答案
  • 这并没有给出结果,父容器无论如何都应该拉伸到全宽,子容器也应该是全宽,但是图标应该在文本旁边并且不远从它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 2012-12-08
相关资源
最近更新 更多