【问题标题】:How to increase radio button size using LinearLayout如何使用 LinearLayout 增加单选按钮的大小
【发布时间】:2017-09-20 09:54:16
【问题描述】:

我正在尝试增加 LinearLayoutRadioButton 的大小,该大小包含在 ScrollView 中,但它无法以任何方式工作。我尝试使用.setScaleX().setScaleY() 之类的属性,但没有成功。这是我的代码:

layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent" android:weightSum="1">



    <LinearLayout
            android:paddingTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:orientation="horizontal">
        <TextView
                android:text="Current location: "
                android:textSize="16sp"
                android:textStyle="bold"
                android:paddingRight="5sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView
                android:id="@+id/current_location"
                android:textSize="16sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </LinearLayout>


    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:sca
                android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/zone_types_list_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:orientation="vertical"
            android:background="@drawable/room_type_border"/>
    </ScrollView>

</LinearLayout>

使用 radioGroup 创建单选按钮的代码:

private LinearLayout listView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  listView = (LinearLayout) rootView.findViewById(R.id.zone_types_list_View);
}

private void addRadioButtons() {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    radioGroup = new RadioGroup(mActivity);
                    for (String ss : typesList) {
                        radioButton = new RadioButton(mActivity);
                        radioButton.setText(ss);
                        radioButton.setTag(ss.trim());
                        radioGroup.addView(radioButton);
                        radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    String tag = (String) buttonView.getTag();
                                    Log.i(TAG, "selected radio: " + tag);
                                    submitZoneType(tag.trim());
                                }
                            }
                        });
                    }
               /*     radioGroup.setPivotX(0);
                    radioGroup.setPivotY(0);

                    radioGroup.setScaleX(2f);
                    radioGroup.setScaleY(2f); */
                    listView.addView(radioGroup);
                }
            });
        }
    }

如果我动态添加radioGroup.setScaleX(2f),它可以帮助我增加RadioButtons 的大小,但随后buttons 的其余部分隐藏在layout 中并且滚动不起作用。我不明白为什么滚动不能这样工作。有没有其他方法可以增加RadioButtons 的大小?

【问题讨论】:

  • 你能检查一下我的答案吗?@user565

标签: android android-layout android-radiogroup


【解决方案1】:

成功了

radioButton.setHeight(10);
radioButton.setWidth(5);

【讨论】:

  • 那么 ScrollView 不会垂直滚动单选按钮大小增加的内容。所以单选按钮的其余部分隐藏在视图上
  • 你在 android:layout_width="match_parent" android:layout_height="fill_parent" 里面设置了滚动视图
  • 这个还是不行
【解决方案2】:

试试这个。

在您的代码中使用radioButton.setLayoutParams(new LinearLayout.LayoutParams(25, 25));

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.your_layout);
RadioButton radioButton = new RadioButton(mActivity);
// you can change the width and height
radioButton.setLayoutParams(new LinearLayout.LayoutParams(25, 25));
linearLayout.addView(radioButton);

注意

你可以改变它的宽度、高度和重量。

view.setLayoutParams(new LinearLayout.LayoutParams(width, height, weight));

【讨论】:

    猜你喜欢
    • 2014-07-03
    • 2011-11-07
    • 2020-06-27
    • 2013-12-30
    • 2014-05-14
    • 2022-01-24
    • 2023-03-25
    • 1970-01-01
    • 2014-08-06
    相关资源
    最近更新 更多