android radiobutton 布局,Android RadioButton实现多行布局

因为RadioGroup继承自LinearLayout,所以所有RadioButton要么纵向排成一行,要么横向排成一行。如果想把RadioButton分成两行,可能会想到使用下面的XML布局,就是用两个LinearLayout。

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="8dp"

android:text="@string/type" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/new_macbook" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/macbook_air" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/macbook_pro" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/imac" />

上面的运行结果如下图这样,可以把RadioButton分为多行,但是这样的RadioGroup会失去单选的功能。因为所有RadioButton必须作为RadioGroup的子View才行。

2c2a44d69f24?nomobile=yes

于是就想了个注意,如果要把所有RadioButton放在两行,就弄两个RadioGroup。然后在其中某个RadioGroup被选中时,清除另一个RadioGroup的选中状态。实现如下:

XML:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="8dp"

android:text="@string/type" />

android:id="@+id/rg1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/rb1"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/new_macbook" />

android:id="@+id/rb2"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/macbook_air" />

android:id="@+id/rg2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/rb3"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/macbook_pro" />

android:id="@+id/rb4"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/imac" />

Java:

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.CompoundButton;

import android.widget.RadioButton;

import android.widget.RadioGroup;

/**

* Created by xuhongchuan on 16/4/30.

*/

public class PriceActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{

private RadioGroup mRg1;

private RadioGroup mRg2;

private RadioButton mRb1;

private RadioButton mRb2;

private RadioButton mRb3;

private RadioButton mRb4;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_price);

initView();

}

private void initView() {

mRg1 = (RadioGroup) findViewById(R.id.rg1);

mRg2 = (RadioGroup) findViewById(R.id.rg2);

mRb1 = (RadioButton) findViewById(R.id.rb1);

mRb2 = (RadioButton) findViewById(R.id.rb2);

mRb3 = (RadioButton) findViewById(R.id.rb3);

mRb4 = (RadioButton) findViewById(R.id.rb4);

mRb1.setOnCheckedChangeListener(this);

mRb2.setOnCheckedChangeListener(this);

mRb3.setOnCheckedChangeListener(this);

mRb4.setOnCheckedChangeListener(this);

}

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

switch (buttonView.getId()) {

case R.id.rb1:

mRg2.clearCheck();

break;

case R.id.rb2:

mRg2.clearCheck();

break;

case R.id.rb3:

mRg1.clearCheck();

break;

case R.id.rb4:

mRg1.clearCheck();

break;

default:

break;

}

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值