优化布局的方式
方式一、
include标签
如果一个程序中每个页面都有TitleBar 我们就不需要每个页面有需要写TitleBar 只需要在一个xml文件中完成TitleBar 通过include引用 提高布局的重用性 修改的时候更方便
方式二、
ViewStub标签
在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局。那么最通常的想法就是把可能用到的View都写在上面,
先把它们的可见性都设为View.GONE,然后在代码中动态的更改它的可见性。这样的做法的优点是逻辑简单而且控制起来比较灵活。但是它的缺点就是,耗费资源。
虽然把View的初始可见View.GONE但是在Inflate布局的时候View仍然会被Inflate,也就是说仍然会创建对象,会被实例化,会被设置属性。也就是说,会耗费内存等资源。
对View的引用并实现延迟加载 它不仅不可视,而且大小为0
显示的方式:
1.找到该组件 并setVisibility(View.VISIBLE) //方法使显示
2.找到该组件 通过组建的inflate()方法显示出来
区别:
通过第二种方式显示出来可以查找到子组件
例如:
View inflateView=mViewStudio.inflate();
TextView tv=(TextView)inflateView.findViewById(R.id.tv);
tv.settext("Hello World");
Exception
如果调用多次会报java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent异常
解决方案
ViewStub的inflate方法 只能调用一次
merge标签
主要作用就是减少父容器的嵌套问题
主容器: (相当于root)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<include layout="@layout/view"/>
</LinearLayout>
子容器:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</merge>
方式一、
include标签
如果一个程序中每个页面都有TitleBar 我们就不需要每个页面有需要写TitleBar 只需要在一个xml文件中完成TitleBar 通过include引用 提高布局的重用性 修改的时候更方便
方式二、
ViewStub标签
在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局。那么最通常的想法就是把可能用到的View都写在上面,
先把它们的可见性都设为View.GONE,然后在代码中动态的更改它的可见性。这样的做法的优点是逻辑简单而且控制起来比较灵活。但是它的缺点就是,耗费资源。
虽然把View的初始可见View.GONE但是在Inflate布局的时候View仍然会被Inflate,也就是说仍然会创建对象,会被实例化,会被设置属性。也就是说,会耗费内存等资源。
对View的引用并实现延迟加载 它不仅不可视,而且大小为0
显示的方式:
1.找到该组件 并setVisibility(View.VISIBLE) //方法使显示
2.找到该组件 通过组建的inflate()方法显示出来
区别:
通过第二种方式显示出来可以查找到子组件
例如:
View inflateView=mViewStudio.inflate();
TextView tv=(TextView)inflateView.findViewById(R.id.tv);
tv.settext("Hello World");
Exception
如果调用多次会报java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent异常
解决方案
ViewStub的inflate方法 只能调用一次
Exception
java.lang.IllegalArgumentException: ViewStub must have a valid layoutResource
解决方案
ViewStub 应该是这样 android:layout
include 应该是layout
所以要注意下
merge标签
主要作用就是减少父容器的嵌套问题
主容器: (相当于root)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<include layout="@layout/view"/>
</LinearLayout>
子容器:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</merge>

被折叠的 条评论
为什么被折叠?



