2

我试图将 s 放在RadioButton一些 s 下方TextView并将它们水平居中,基本上将标签移动到按钮上方。

这是我的代码:

XML:

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal"
    android:id="@+id/someRadioGroup"/>

爪哇:

LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(VERTICAL);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);

TextView choiceTextView = new TextView(context);
choiceTextView.setText("1");
choiceTextView.setLayoutParams(layoutParams);
choiceTextView.setGravity(Gravity.CENTER);
choiceLinearLayout.addView(choiceTextView);

RadioButton choiceRadioButton = new RadioButton(context);
choiceRadioButton.setText("");
choiceRadioButton.setGravity(Gravity.CENTER);
choiceRadioButton.setLayoutParams(layoutParams);
choiceLinearLayout.addView(choiceRadioButton);

someRadioGroup.addView(choiceLinearLayout);

请注意,上面的代码在循环中添加了七个选项中的每一个。

这是它在 MOST 设备上的样子(在 Android 2.3、4.3 和 4.4 上测试):

大多数设备

这是它在 Android 4.1 上的样子:

和_41

请注意,TextViews 实际上并没有偏离中心 - 它们完全居中。s离RadioButton得太远了。

我能做些什么来解决这个问题?

编辑:

我已经添加choiceTextView.setGravity(Gravity.CENTER);到上面的代码中。它没有做任何事情,因为文本已经居中。文字刚刚好。RadioButtons 离左边太远了。这是在我的设备上启用了布局边界选项的屏幕截图:

display_layouts

4

2 回答 2

0
    //This layout is to group the options
LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);

//You can use a cicle
for (int i = 0; array.size(); i++){
    //This layout is to group the label and radiobutton.
    LinearLayout radioLayout = new LinearLayout(context);
    radioLayout.setGravity(Gravity.CENTER);
    radioLayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams radioParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    radioLayout.setLayoutParams(radioParams);
    TextView choiceTextView = new TextView(context);
    choiceTextView.setText(i);
    radioLayout.addView(choiceTextView);
    RadioButton choiceRadio = new RadioButton(context);
    radioLayout.addView(choiceRadio);
    choiceLinearLayout.addView(radioLayout);
}


RadioButton choiceRadioButton = new RadioButton(context);
choiceRadioButton.setText("");
choiceRadioButton.setGravity(Gravity.CENTER);
choiceRadioButton.setLayoutParams(layoutParams);
choiceLinearLayout.addView(choiceRadioButton);

someRadioGroup.addView(choiceLinearLayout);
于 2014-05-14T17:47:02.117 回答
0

我最终得到了一个不同的解决方案 - 使用ToggleButtons 而不是RadioButtons。我将 a 设置StateListDrawable为切换按钮的背景,并确保文本始终为空字符串,无论按钮是打开还是关闭。这是我最终得到的代码:

LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(VERTICAL);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);

TextView choiceTextView = new TextView(context);
choiceTextView.setText("1");
choiceTextView.setLayoutParams(layoutParams);
choiceTextView.setGravity(Gravity.CENTER);
choiceLinearLayout.addView(choiceTextView);

ToggleButton choiceToggleButton = new ToggleButton(context);
choiceToggleButton.setText("");
choiceToggleButton.setTextOn("");
choiceToggleButton.setTextOff("");
choiceToggleButton.setGravity(Gravity.CENTER);

StateListDrawable radioDrawable = getRadioDrawable(context); // this function creates the state list our of pngs that I've added to the project

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
    choiceToggleButton.setBackgroundDrawable(radioDrawable);
} else {
    choiceToggleButton.setBackground(radioDrawable);
}

LinearLayout.LayoutParams choiceToggleButtonLayoutParams = new LayoutParams(radioDrawable.getIntrinsicWidth(), radioDrawable.getIntrinsicHeight());

choiceToggleButton.setLayoutParams(choiceToggleButtonLayoutParams);
choiceLinearLayout.addView(choiceToggleButton);

choiceToggleButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        ToggleButton toggleButton = (ToggleButton) view;

        // do not allow toggling a button off
        if (!toggleButton.isChecked()) {
            toggleButton.setChecked(true);
        }

        // uncheck all other buttons, leaving the current one checked
        for (int i = 0; i < someRadioGroup.getChildCount(); i++) {
            LinearLayout linearLayout = (LinearLayout) someRadioGroup.getChildAt(i);
            if (linearLayout != null) {
                ToggleButton tb = (ToggleButton) linearLayout.getChildAt(1);
                if (tb != null && tb != toggleButton) {
                    tb.setChecked(false);
                }
            }
        }
    }
});

someRadioGroup.addView(choiceLinearLayout);

请注意,OnClickListener每个都需要一个ToggleButton来模仿正确的RadioButton行为。

这是 Android 4.1 上的结果(每个都应用了一些左右边距ToggleButton):

有用

于 2014-05-15T22:49:28.620 回答