布局优化

include

将指定布局文件加载到当前布局文件

注意:
必须要复写android:layoutwidth和android:layoutheight属性才能使用其它属性,比如:android:layoutgrivity、android:layoutalign…、android:id等
建议将给include标签调用布局设置宽高、位置、ID等工作放在调用布局的根标签中,这样可以避免给include标签设置属性不当造成的各种问题,给include标签设置android:id属性后,程序实例化子布局中组件失败的现象

共用布局 include_voice_ctrl_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="61dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <!-- 播放、暂停 -->
    <Button
        android:id="@+id/voiceBtnId"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="100"
        android:background="@drawable/voice_btn_selector"
        android:gravity="center"
        android:onClick="onClick"
        android:text="@string/voice_stop"
        android:textColor="@android:color/white"
        android:textSize="24sp" />
    ...
   
</LinearLayout>

导入布局

<include layout="@layout/include_voice_ctrl_bar_layout" />

merge

在Android中通过使用merge能够减少视图的节点数

注意:
merge必须放在布局文件的根节点上。
merge并不是一个ViewGroup,也不是一个View,它相当于声明了一些视图,等待被添加。
merge标签被添加到A容器下,那么merge下的所有视图将被添加到A容器下。
因为merge标签并不是View,所以在通过LayoutInflate.inflate方法渲染的时候, 第二个参数必须指定一个父容器,且第三个参数必须为true,也就是必须为merge下的视图指定一个父亲节点。
如果Activity的布局文件根节点是FrameLayout,可以替换为merge标签,这样,执行setContentView之后,会减少一层FrameLayout节点。
自定义View如果继承LinearLayout,建议让自定义View的布局文件根节点设置成merge,这样能少一层结点。
因为merge不是View,所以对merge标签设置的所有属性都是无效的。

示例在竖直显示的自定义view中使用merge

<?xml version="1.0" encoding="utf-8"?>
<!-- 习惯性的标记一下,MergeLayout布局 android:orientation="vertical" -->
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:background="#000000"
        android:gravity="center"
        android:text="第一个TextView"
        android:textColor="#ffffff" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:background="#ffffff"
        android:gravity="center"
        android:text="第一个TextView"
        android:textColor="#000000" />

</merge>
/**
 * 自定义的View,竖直方向的LinearLayout
 */
public class MergeLayout extends LinearLayout {

    public MergeLayout(Context context) {
        super(context);
        // 设置为数值方向的布局
        setOrientation(VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.merge_activity, this, true);
    }
}

viewStub

ViewStub使用的是惰性加载的方式,即使将其放置于布局文件中,如果没有进行加载那就为空

那ViewStub适用于场景呢?
通常用于网络请求页面失败的显示。使用ViewStub,在需要的时候才进行加载

注意

ViewStub只能加载一次,若调用两次inflate()的话会导致异常。因为viewStub加载其下子view,就从父布局中移除,并添加弱引用进行GC回收,每次加载节点会检查resourceLayout是否为空,为空则抛出异常。
ViewStub在加载完成之后会移除自身,并把自身的内容转给父布局,所以此时viewStub中的内容已经不存在了,view已经是父布局的东西了。

 public View inflate() {
        final ViewParent viewParent = getParent();//获取父View

        if (viewParent != null && viewParent instanceof ViewGroup) {
        //若父不是ViewGroup就会抛出异常("ViewStub must have a non-null ViewGroup viewParent")
            if (mLayoutResource != 0) {
            //这个就是ViewStub只能加载一次的原因,第二次加载则抛出异常(throw new IllegalArgumentException("ViewStub must have a valid layoutResource"))
                final ViewGroup parent = (ViewGroup) viewParent;
                final LayoutInflater factory;
                if (mInflater != null) {
                    factory = mInflater;
                } else {
                    factory = LayoutInflater.from(mContext);
                }
                final View view = factory.inflate(mLayoutResource, parent,
                        false);
                //创建一个View,这个View为ViewStub的内容,mLayoutResource为ViewStub自身的Layout资源文件id
                if (mInflatedId != NO_ID) {
                    view.setId(mInflatedId);
                    //若mInflatedId存在,则将id重新赋值给新的View
                }

                final int index = parent.indexOfChild(this);
                parent.removeViewInLayout(this);
                //通过父布局将ViewStub移除
                final ViewGroup.LayoutParams layoutParams = getLayoutParams();
                if (layoutParams != null) {
                    parent.addView(view, index, layoutParams);
                } else {
                    parent.addView(view, index);
                }
                //将ViewStub中的内容添加到父容器中
                mInflatedViewRef = new WeakReference<View>(view);
                //设置为弱引用,当VIewStub设置为空时将立即执行GC
                if (mInflateListener != null) {
                    mInflateListener.onInflate(this, view);
                }

                return view;
                //最后将View返回出去
            } else {
                throw new IllegalArgumentException("ViewStub must have a valid layoutResource");
            }
        } else {
            throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
        }
    }

viewStub的使用示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.myviewstub.MainActivity">
    <ViewStub
        android:id="@+id/vs"
        android:layout="@layout/viewstub"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

viewStub的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/hello_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DATA EMPTY!"/>
</FrameLayout>
public class MainActivity extends AppCompatActivity {
    private ViewStub viewStub;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewStub = (ViewStub) findViewById(R.id.vs);
    }
    public  void inflate(View view){
        viewStub.inflate();
        textView  = (TextView) findViewById(R.id.hello_tv);
    }
    public void setData(View view){
        textView.setText("DATA!!");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值