Java 以编程方式将RadioButton的边距或填充添加到RadioButton组中
我想以编程方式向RadioGroup添加边距或填充,但它不起作用 单选按钮:Java 以编程方式将RadioButton的边距或填充添加到RadioButton组中,java,android,Java,Android,我想以编程方式向RadioGroup添加边距或填充,但它不起作用 单选按钮: <RadioButton xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_fruit" android
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_fruit"
android:button="@null"
android:checked="false" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
</RadioGroup>
RadioButton radioButtonView = (RadioButton) getActivity().getLayoutInflater().inflate(R.layout.radio_button, null);
radioGroup.addView(radioButtonView);
我尝试使用LayoutParams
和dividerPadding
,但不起作用
试试这个
RadioButton radioButtonView = (RadioButton) getLayoutInflater().inflate(R.layout.radio_button, null, false);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(15, 15, 15, 15);
radioButtonView.setLayoutParams(params);
radioGroup.addView(radioButtonView);
单选按钮的边距和填充:
RadioButton rd = new RadioButton(getActivity());
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
rd.setLayoutParams(params);
rd.setPadding(left, top, right, bottom);
radioGroup.addView(rd);
RadioGroup的边距和填充是相同的,只有LayoutParams的类型不同(不是RadioGroup.LayoutParams),但RadioGroup的父布局是什么:LinearLayout.LayoutParams或RelativeLayout.LayoutParams或FrameLayout.LayoutParams等。您明白了
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
radioGroup.setLayoutParams(params);
左、上、右、下都是像素,所以您应该将DP转换为PX,如图所示谢谢!它与新的
布局参数
一起工作。我试图用getLayoutParams()
获取当前的LayoutParams
,但它不起作用。现在它工作了,谢谢!谢谢你的帖子。这就完成了答案:-)