布局中的一些小知识


一、weight 属性使用说明

       weightSum  :在父类的布局中定义 weight 总数,那么子类利用layout_weight 属性来判断占用的布局的百分比。如果不指定,那么就按所有的layout_weight总和来确定。

       在利用layout_weight的时候,需要宽度或者是高度 来由layout_weight 来决定的,就不要指定相关的数值。


二、延迟加载和避免代码重复

    1、避免代码重复

        <include />

     2、ViewStub    实现View的延迟加载

         直接上实例:

         onCreate方法里

                    Button bt = (Button)findViewById(R.id.bt);
        vs =(ViewStub)findViewById(R.id.vs1);
        bt.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(viewId ==null){
                viewId=vs.inflate();
                }
            }
        });
        
        Button bt2 =(Button)findViewById(R.id.bt2);
        bt2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                viewId.setVisibility(View.INVISIBLE);
            }
        });


布局:

           <Button  android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top"/>

        <Button  android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt"/>
        
        <ViewStub android:id="@+id/vs1"
            android:layout="@layout/myviewstub"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#8800FF00"
            android:inflatedId="@+id/text"/>
        
          <ViewStub android:id="@+id/vs1"
            android:layout="@layout/myviewstub"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#8800FF00"
            android:inflatedId="@+id/text2"/>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值