Android 在代码中书写布局(xml)配置

1.使用代码编写一个底部选项卡的布局

Android 在代码中书写布局(xml)配置_第1张图片

2.整个页面的容器布局(包含Fargment,分割线,选项卡)

    private void initView(Context context) {
        setBackgroundColor(0xfff6f6f6);
        FrameLayout frameLayout=new FrameLayout(context);//选项界面容器
        frameLayout.setId(FL_ID);
        View lineView=new View(context);//分割线
        lineView.setId(LINE_ID);
        RelativeLayout.LayoutParams rlParams=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        rlParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        rlParams.addRule(RelativeLayout.ABOVE , LINE_ID);

        lineView.setBackgroundColor(lineColor);
        RelativeLayout.LayoutParams rlParams2=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, DensityUtils.dip2px(context, 1));
        rlParams2.addRule(RelativeLayout.ABOVE , TAB_ID);

        addView(frameLayout, rlParams);//选项界面容器
        addView(lineView,rlParams2);//分割线

        //选项卡容器
        linearLayout=new LinearLayout(context);
        linearLayout.setBackgroundColor(tabBgColor);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL);
        linearLayout.setId(TAB_ID);
        tabNum=tabIcos.length;
        for (int i = 0; i < tabNum; i++) {
            View view = createIndicator(tabIcos[i], tabtxts[i], tabItemTvColor, "itemTag"+i, "icoTag" + i, "txtTag" + i);
            view.setOnClickListener(OnClick);
            if(i== nowTabIndex){//初始化选项卡
                changeTab(view, i);
            }
            linearLayout.addView(view);
        }
        RelativeLayout.LayoutParams rlParams3=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, DensityUtils.dip2px(context, 50));
        rlParams3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addView(linearLayout,rlParams3);//选项卡容器
    }

3.选项卡条目控件布局(RelaticeLayout中包含图标,小红点,与相应文本)

Android 在代码中书写布局(xml)配置_第2张图片

/**
     * 创建子选项卡(图片资源,显示文字,文字颜色,条目标记,图标标记,文本标记)
     */
    private View createIndicator(int iconResID, int tvResID, int tvColor, String itemTag, String iconTag, String textTag) {
        RelativeLayout view = new RelativeLayout(getContext());//单个选项卡的容器
        view.setTag(itemTag);
        view.setGravity(Gravity.CENTER_HORIZONTAL);
        LinearLayout.LayoutParams relParams=new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
        view.setLayoutParams(relParams);

        ImageView iconView = new ImageView(getContext());//创建一个图标控件
        iconView.setTag(iconTag);
        iconView.setId(ICO_ID);
        RelativeLayout.LayoutParams rlParams5=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rlParams5.addRule(RelativeLayout.BELOW, DTO_ID);
        iconView.setImageResource(iconResID);

        ImageView dotView = new ImageView(getContext());//创建一个红点控件
        dotView.setId(DTO_ID);
        RelativeLayout.LayoutParams rlParams3=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rlParams3.addRule(RelativeLayout.RIGHT_OF, ICO_ID);
        rlParams3.leftMargin=0-12;
        rlParams3.bottomMargin=0-12;
        dotView.setImageResource(R.mipmap.red_dot);
        dotView.setVisibility(INVISIBLE);//默认不显示

        TextView textView = new TextView(getContext());//创建一个文本控件
        textView.setTag(textTag);
        textView.setTextColor(tvColor);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        textView.setText(tvResID);
        RelativeLayout.LayoutParams rlParams4=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rlParams4.addRule(RelativeLayout.BELOW , ICO_ID);

        view.addView(iconView, rlParams5);
        view.addView(textView, rlParams4);
        view.addView(dotView,rlParams3);

        return view;
    }

4.参考方法(参考: http://blog.csdn.net/androiddevelop/article/details/8426935)

(1) Margin 配置

    android:layout_marginTop  
    android:layout_marginBottom  
    android:layout_marginLeft  
    android:layout_marginRight  
对应代码

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
    // 单位是px  
    layoutParams.topMargin = 66;  
    layoutParams.bottomMargin = 66;  
    layoutParams.leftMargin = 66;  
    layoutParams.rightMargin = 66;  

(2)相对(ID)位置 配置

android:layout_above  
android:layout_below  
android:layout_toLeftOf  
android:layout_toRightOf 
对应代码

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   layoutParams.addRule(RelativeLayout.ABOVE, R.id.viewId);  
    layoutParams.addRule(RelativeLayout.BELOW, R.id.viewId);  
    layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.viewId);  
    layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.viewId);  

(3)与边(ID)对齐

android:layout_alignBaseline  
android:layout_alignTop  
android:layout_alignBottom  
android:layout_alignLeft  
android:layout_alignRight 
对应代码

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  
layoutParams.addRule(RelativeLayout.ALIGN_BASELINE, R.id.viewId);  
layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.viewId);  
layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.viewId);  
layoutParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.viewId);  
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.viewId);


(4)对齐父容器边缘

    android:layout_alignParentTop  
    android:layout_alignParentBottom  
    android:layout_alignParentLeft  
    android:layout_alignParentRight  
对应代码

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
      
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);  
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  

(5)父容器中居中

    android:layout_centerHorizontal  
    android:layout_centerVertical  
    android:layout_centerInParent  
对应代码

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);  
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);  
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 






你可能感兴趣的:(Android)