英文:
applying layout preferences to dynamically added buttons in android radiogroup
问题
I tried to look into all the possible answers but really I cannot figure how to proceed.
I'm creating radio buttons into a radiogroup dynamically
for (int i = 0; i < keys.length; i++) {
final RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(View.generateViewId());
rdbtn.setText(keys[i]);
rdbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedWH = rdbtn.getText().toString();
}
});
mRgAllButtons.addView(rdbtn);
}
and I would like to apply this style to the buttons
<RadioButton
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="60dp"
android:layout_marginEnd="60dp"
android:background="@drawable/radio_pressed"
android:button="@android:color/transparent"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold" />
Any help is really appreciated.
<details>
<summary>英文:</summary>
I tried to look into all the possible answers but really I cannot figure how to proceed.
I'm creating radio buttons into a radiogroup dinamically
for (int i = 0; i < keys.length; i++) {
final RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(View.generateViewId());
rdbtn.setText(keys[i]);
rdbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedWH = rdbtn.getText().toString();
}
});
mRgAllButtons.addView(rdbtn);
}
and I would like to apply this style to the buttons
<RadioButton
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="60dp"
android:layout_marginEnd="60dp"
android:background="@drawable/radio_pressed"
android:button="@android:color/transparent"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold" />
Any help is really appreciated.
</details>
# 答案1
**得分**: 0
```java
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(你的宽度, 你的高度);
marginLayoutParams.leftMargin = 你的左边距;
marginLayoutParams.rightMargin = 你的右边距;
rdbtn.setLayoutParams(marginLayoutParams);
在你的代码中添加这部分,以便为动态创建的视图设置布局参数,你必须为它创建一个 LayoutParams。在这里,你可以将边距设置为任何你想要的值。
英文:
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(yourWidth, yourHeight);
marginLayoutParams.leftMargin = yourLeftMargin;
marginLayoutParams.rightMargin = yourRightMargin;
rdbtn.setLayoutParams(marginLayoutParams);
add this in your code in order to set layout parameters to your view which you are creating dynamically you must create LayoutParams for it. and there you can set margins to whatever you want.
答案2
得分: 0
这是一个简单的方法。您可以从XML中创建一个RadioButton
视图实例,以便它具有所需的样式。
- 创建带有
RadioButton
元素和样式的XML布局 - 就像您所提到的那样。 - 使用该布局创建一个新的
RadioButton
视图(并将其添加到RadioGroup
中)。
LayoutInflater inflater = LayoutInflater.from(context);
for (int i = 0; i < keys.length; i++) {
final RadioButton rdbtn = inflater.inflate(R.layout.layout_name, mRgAllButtons, false);
// mRgAllButtons - 父级视图。
// attachToParent - false - 手动添加到父级。
rdbtn.setId(View.generateViewId());
rdbtn.setText(keys[i]);
rdbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedWH = rdbtn.getText().toString();
}
});
mRgAllButtons.addView(rdbtn);
}
英文:
This is a simple way. You can create a RadioButton
view instance from the XML so it will have the style that you want.
- Create XML layout with
RadioButton
element and the style - just what you have mentioned. - Use that layout to create a new RadioButton View (and add it to the RadioGroup).
LayoutInflater inflater = LayoutInflater.from(context);
for (int i = 0; i < keys.length; i++) {
final RadioButton rdbtn = inflater.inflate(R.layout.layout_name, mRgAllButtons, false);
// mRgAllButtons - parent of this view.
// attachToParent - false - add to the parent manually.
rdbtn.setId(View.generateViewId());
rdbtn.setText(keys[i]);
rdbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedWH = rdbtn.getText().toString();
}
});
mRgAllButtons.addView(rdbtn);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论