我有一个radiobutton
,右边带有文本和图片,我希望width
是parent_width
,但是drawableright
接近页面上的文本对。我该如何实现?
我得到以下
<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>
答案 0 :(得分: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>
答案 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>