欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Android 流式布局 RadioGroup,androidradiogroup,1.使用方法 ```ja

来源: javaer 分享于  点击 3504 次 点评:143

Android 流式布局 RadioGroup,androidradiogroup,1.使用方法 ```ja


1.使用方法 ```javapackage com.example.radiogroup;

import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.graphics.Color;import android.view.ViewGroup.LayoutParams;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

private SelfRadioGroup mRadioLayout;private List<String> mSelectDataList;private RadioButton radioButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    mRadioLayout = (SelfRadioGroup) findViewById(R.id.items_parent_Single);    initList();    //根据内容的数量添加RadioButton    for (int i = 0; i < mSelectDataList.size(); i++) {        radioButton = new RadioButton(this);        radioButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));        radioButton.setTextColor(Color.BLACK);        radioButton.setText(mSelectDataList.get(i));        mRadioLayout.addView(radioButton);    }    mRadioLayout.setOnCheckedChangeListener(new OnCheckedChangeListener() {        @Override        public void onCheckedChanged(RadioGroup group, int checkedId) {            int id = group.getCheckedRadioButtonId();            RadioButton by = (RadioButton) findViewById(id);            System.out.println("选择的内容:"+by.getText());        }    });}/** * 获取RadioButton内容 */private void initList() {    mSelectDataList = new ArrayList<String>();    mSelectDataList.add("批发了贷款");    mSelectDataList.add("部件");    mSelectDataList.add("文明行政村");    mSelectDataList.add("单位综合检查");    mSelectDataList.add("单位");    mSelectDataList.add("事件");    mSelectDataList.add("门前三包");    mSelectDataList.add("批发了贷款");    mSelectDataList.add("批发");}

}

None``````<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:scrollbars="none">

<com.example.radiogroup.SelfRadioGroup    android:id="@+id/items_parent_Single"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:visibility="visible" />

</ScrollView>2.自定义RadioGroupjavapackage com.example.radiogroup;

import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.RadioGroup;

/* * 自定义RadioGroup * @author ZhangZhaoCheng /public class SelfRadioGroup extends RadioGroup { private Context mContext;

public SelfRadioGroup(Context context) {    super(context);    mContext = context;}public SelfRadioGroup(Context context, AttributeSet attrs) {    super(context, attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int height = 0;    int width = 0;    int rows = 0;    /**     * 获得此ViewGroup上级容器为其推荐的宽和高,以及计算模式     */    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);    int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);    // 计算出所有的childView的宽和高    measureChildren(widthMeasureSpec, heightMeasureSpec);    for (int index = 0; index < getChildCount(); index++) {        final View child = this.getChildAt(index);        width += child.getMeasuredWidth();        if (rows == 0) {            height = Math.max(height, child.getMeasuredHeight());        }        //如果数据的宽度大于整个屏幕的宽度 row行数++         if (width > sizeWidth) {            rows++;            height += child.getMeasuredHeight();            width = child.getMeasuredWidth();        }    }    setMeasuredDimension(sizeWidth, height);}@Overrideprotected void onLayout(boolean arg0, int left, int top, int right,        int bottom) {    final int count = getChildCount();    int width = left;    int height = top;    int row = 0;    int lastBottom = 0;    for (int i = 0; i < count; i++) {        final View child = this.getChildAt(i);        int childWidth = child.getMeasuredWidth();        int childHeight = child.getMeasuredHeight();        int cl = 0, ct = 0, cr = 0, cb = 0;        width += childWidth;        if (row == 0) {            height = childHeight;        }        if (width > right) {            width = left + childWidth;            height = lastBottom + childHeight;            row++;        }        cl = width - childWidth;        ct = height - childHeight;        cr = width;        cb = height;        child.layout(cl, ct, cr, cb);        lastBottom = cb;    }}

}

```

相关栏目:

用户点评