/**生成顶部导航**/
public void initRadioButton(final List list){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
//屏幕的宽度
width = dm.widthPixels;
//分辨率
density = dm.density;
//每个分类的宽度
itemWidth = (width-20)/3;
//分类的个数
int size = list.size();
radioGroup.removeAllViews();
//if (size>1) {
if (size>0) {
int total = width;
if (itemWidth*size+(size-1)*10+20>width) {
total = itemWidth*size+(size-1)*10+20; //每个分类的宽度+分类的间距+左右两边的间距
}
horizontalScrollView.setLayoutParams(new LinearLayout.LayoutParams(-1, (int) (50*density)) );
HorizontalScrollView.LayoutParams layoutParams = new HorizontalScrollView.LayoutParams(total, (int) (40*density));
layoutParams.leftMargin=10;
layoutParams.rightMargin=10;
layoutParams.bottomMargin=5;
//layoutParams.topMargin=(int) (10*density);
layoutParams.gravity=Gravity.BOTTOM;
radioGroup.setLayoutParams(layoutParams);
horizontalScrollView.setVisibility(View.VISIBLE);
RadioButton radioButton = null;
InforSubBean2 childVO;
for (int i = 0; i < size; i++) {
childVO = list.get(i);
if (childVO==null) {
return;
}
radioButton= (RadioButton) LayoutInflater.from(getApplicationContext()).inflate(R.layout.radiobuttton, null);
if (i==0) {
radioButton.setChecked(true);
sub_id = list.get(0).moduleId;
System.out.println("----------------0");
request_sub_list();
}
radioButton.setId(i);
radioButton.setText(childVO.moduleName);
radioGroup.addView(radioButton);
radioButton.setLayoutParams(new RadioGroup.LayoutParams(itemWidth,(int) (40*density)));}
布局文件<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="@drawable/top_bg"
android:orientation="vertical" >
android:id="@+id/mailbox_horizontalscrollView"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/top_bg"
android:scrollbars="none"
android:visibility="gone" >
android:id="@+id/mailbox_title_group"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radio"
android:layout_width="90dip"
android:layout_height="40dip"
android:gravity="center"
android:button="@null"
android:background="@drawable/radio_select"
android:text="分类"
/>
核心代码就是这上面的