应用布局首选项到动态添加的按钮在Android的RadioGroup中。

huangapple go评论67阅读模式
英文:

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&#39;m creating radio buttons into a radiogroup dinamically

    for (int i = 0; i &lt; 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

    &lt;RadioButton
                
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;40dp&quot;
                android:layout_marginStart=&quot;60dp&quot;
                android:layout_marginEnd=&quot;60dp&quot;
                android:background=&quot;@drawable/radio_pressed&quot;
                android:button=&quot;@android:color/transparent&quot;                
                android:textAlignment=&quot;center&quot;
                android:textColor=&quot;@color/colorPrimary&quot;
                android:textSize=&quot;18sp&quot;
                android:textStyle=&quot;bold&quot; /&gt;

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视图实例,以便它具有所需的样式。

  1. 创建带有RadioButton元素和样式的XML布局 - 就像您所提到的那样。
  2. 使用该布局创建一个新的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.

  1. Create XML layout with RadioButton element and the style - just what you have mentioned.
  2. 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 &lt; 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);
}

huangapple
  • 本文由 发表于 2020年4月8日 20:28:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定