android导航点自动生成,android 动态生成顶部导航栏

/**生成顶部导航**/

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="分类"

/>

核心代码就是这上面的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值