zoukankan      html  css  js  c++  java
  • android 学习Layout布局的使用

     android 常用布局

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

    RelativeLaytout(相对布局)    
    最灵活的

    TableLayout(表格布局)     
    使用GridView代替

    AbsoluteLayout(绝对布局)    
    最好不要使用 绝对坐标

    Framelayout(帧布局)      
    布局叠加时使用 比如视频缓冲的环形滚动条

    使用频次
    Absolute<Table<Frame<Linear<Relative
    android布局原则
    (1)尽量多的使用线性布局和相对布局不要使用绝对布局
    (2)在布局层次一样的情况下,建议使用线性布局代替相对布局因为线性布局性能稍高一点
    (3)将可复用的组件抽取出来,并通过include标签使用
    (4)使用viewstub标签加载一些不常用的布局 (暂时不使用的)
    (5)使用merge标签减少标签的嵌套层次
    
    
    <include/>的使用
    作用:将公用的组件抽取出来单独一个xml文件,然后使用include标签导入公共布局
    效果:提高ui的制作和复用效率
    通过findViewById 也可以找到view 因为通过include实际上是将自布局直接包含进公共布局当中

    1,子布局title.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="30dp"
        android:background="#243821">
    </LinearLayout>

      2,主布局main.xml

    <LinearLayout
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical">
        <include layout="@layout/title"
            ></include>
    </LinearLayout>
    
    
    使用merge合并ui布局
    作用:合并ui布局,使用该布局能降低ui布局的嵌套层次
    场景1;布局根节点是FrameLayout且不需要设置backgroud和padding等属性,可以用merge代替
    场景2:某布局作为自布局被其他布局include时,使用merge当做该布局的顶接点,这样再被引入时,
    顶接点会自动被忽略
    1,子布局progress.xml
    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
    
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/progress"
            android:layout_gravity="center"/>
    </merge>

      2,主布局main.xml

    <LinearLayout
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical">
        <include layout="@layout/title"
            ></include>
    
        <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
            <include layout="@layout/progress"/>
        </FrameLayout>
    </LinearLayout>
    
    
    使用viewstub惰性加载
    作用:viewstub标签同incude标签一样可以用来引入一个外部的布局,不同的是viewstub引入的布局默认不会扩张,
    既不会占用显示也不会占用位置,从而在解析laytou时,节省cpu和内存
    1,子布局common.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/text"
            android:text="viewstub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

      2,主布局main.xml

    <LinearLayout
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical">
        <include layout="@layout/title"
            ></include>
    
        <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
            <include layout="@layout/progress"/>
        </FrameLayout>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn"
            android:text="按钮"
            />
        <ViewStub
            android:id="@+id/viewStub"
            android:layout="@layout/common"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

      3,MainActivity

    public class MainActivity extends Activity {
    
        private Button btn ;
        private ViewStub viewStub;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn = (Button)findViewById(R.id.btn);
            viewStub = (ViewStub)findViewById(R.id.viewStub);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewStub.inflate();
                }
            });
        }
    }
     
      
     
     
  • 相关阅读:
    大数乘法的几种算法分析及比较(2014腾讯南京笔试题)
    【经典数据结构】Trie
    [LeetCode] MaximumDepth of Binary Tree
    [LeetCode] Minimum Depth of Binary Tree
    二叉树相关题目总结
    python之函数基础总结
    python基础之文件处理总结
    利用for循环和range输出9 * 9乘法口诀表
    购物车程序作业
    字典练习
  • 原文地址:https://www.cnblogs.com/techdreaming/p/5008431.html
Copyright © 2011-2022 走看看