Android 流式布局 RadioGroup
发布时间:2020-05-24 21:42:20 所属栏目:Java 来源:互联网
导读:Android 流式布局 RadioGroup
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 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.自定义RadioGrouppackage 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;
}
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
