如何在布局中完全水平居中没有文字的单选按钮?

时间:2014-05-14 17:35:30

标签: android android-layout android-4.0-ice-cream-sandwich android-radiobutton

我正在尝试将RadioButton放在某些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上测试):

most_devices

这是Android 4.1上的样子:

and_41

请注意,TextView实际上并非偏离中心 - 它们完全居中。这是RadioButton s太远了。

我该怎么做才能解决这个问题?

编辑:

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

display_layouts

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

我最后得到了一个不同的解决方案 - 使用ToggleButton而不是RadioButton s。我将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都应用了一些左右边距):

it_works