Android 流式布局 RadioGroup
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;
}
}
}
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!