安卓布局优化

推介使用布局

线性布局 (LinearLayout) 线性的 垂直的 水平的

相对布局(RelativeLayout) 最灵活的

表格布局(TableLayout) 像表格一样有行有列 使用GridView代替

绝对布局(AbsoluteLayout)最好不要使用 位置都是绝对的,不会根据屏幕进行改变

帧布局(FrameLayout)布局叠加使用按照使用量由低到高的排列

布局优化

image.png

image.png

image.png

创建一个公共的头文件xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="#000"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/back"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:textSize="14dp"
        android:text="返回"
        android:layout_centerVertical="true"
        android:textColor="#f40000"/>
    <TextView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/content"
        android:text="布局优化"
        android:textSize="14dp"
        android:textColor="#fff"/>
    <TextView
        android:text="功能界面"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text3"
        android:textSize="14dp"
        android:textColor="#fff"/>

</RelativeLayout>

然后在用到该文件的地方引入

<include layout="@layout/common_title"></include>
image.png

使用merge合并UI布局

image.png

没有改变之前


image.png
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressbar"
        android:layout_gravity="center"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请稍后"
        android:layout_gravity="center"
        android:id="@+id/textprogress"/>

</merge>
<FrameLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       >
       <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/text3"
           android:text="正文内容"/>
       <include layout="@layout/common_progress"></include>
   </FrameLayout>

改变成merge之后

image.png

标签重叠啦

布局优化之ViewStub

image.png

创建一个新的公共的xml文件在ViewStub标签地方使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tectview"
        android:text="隐藏内容"/>
</LinearLayout>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="显示隐藏"/>
    <ViewStub
        android:layout_width="match_parent"
        android:id="@+id/viewStub"
        android:layout="@layout/common_text"
        android:layout_height="wrap_content" />

在java代码中实现功能,点击按钮,显示内容viewStub.inflate();
inflate只能初始化一次,如果想要点击按钮显示隐藏则需要判断,是否是处于inflate状态,如果是获取到在隐藏

 button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                viewStub.inflate();
            }
        });
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容