[英]Add margin or padding to RadioButton into a RadioGroup programmatically
我想以編程方式向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:button="@null"
android:checked="false" />
RadioGroup中:
<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>
Java代碼:
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的邊距和填充:
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,就像在這個答案中一樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.