加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android 流式布局 RadioGroup

发布时间:2020-12-15 03:21:07 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 1.使用方法 package com.example.radiogroup;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activ

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

1.使用方法

package 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;
	
	@Override
	protected 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("批发");
	}
}


 
 
<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.自定义RadioGroup

package 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);
	}

	@Override
	protected 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);
	}

	@Override
	protected 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;
		}
	}
}


以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读