Advertisement

Android------布局(Layout)

阅读量:

布局是指页面的控件的摆放样式。
在Android中使用**.xml格式** 来控制布局。

一、布局分类:

1、线性布局

2、相对布局

二、布局的优化技巧:

1、比例划分(线性布局特有)

2、布局嵌套


一、布局分类:

1、线性布局(LinearLayout):内部的控件全部横向或者纵向一字排布。

这里只介绍几个基本的属性设置,刚开始学也没有贪多。

设置方向:

复制代码
 android:orientation="horizontal" //横向排布

    
  
    
 android:orientation="vertical"   //纵向排布

设置背景颜色(可参考RGB色表:https://www.sioe.cn/yingyong/yanse-rgb-16/

复制代码
    android:background="#9400D3"

设置外边距

复制代码
 //同时设置四个方向的外边距

    
 android:layout_margin="10dp" //本元素离上下左右间的距离
    
  
    
 //也可以单独设置某个方向的外边距
    
 android:layout_marginBottom    //离某元素底边缘的距离
    
 android:layout_marginLeft    //离某元素左边缘的距离
    
 android:layout_marginRight    //离某元素右边缘的距离
    
 android:layout_marginTop    //离某元素上边缘的距离
    
 android:layout_marginStart    //本元素里开始的位置的距离
    
 android:layout_marginEnd    //本元素里结束位置的距离

设置内边距

复制代码
 //同时设置四个方向的内边距

    
 android:padding    //指定布局与子布局的间距
    
  
    
 //也可以单独设置某个方向的内边距  
    
 android:paddingLeft    //指定布局左边与子布局的间距
    
 android:paddingTop    //指定布局上边与子布局的间距
    
 android:paddingRight    //指定布局右边与子布局的间距
    
 android:paddingBottom    //指定布局下边与子布局的间距
    
 android:paddingStart    //指定布局左边与子布局的间距与android:paddingLeft相同
    
 android:paddingEnd    //指定布局右边与子布局的间距与android:paddingRight相同

设置重力方向(一般默认为从左上方开始,符合人们的阅读习惯)

复制代码
    android:gravity="bottom|right"   //设置为在右下方

2、相对布局(RelativeLayout):各内部组件基于选定基准元素进行定位,并位于基准元素指定的方向上。

由于这种布局方式是基于与其他控件互动完成的,并非独立存在;因此,在构建系统时必须对每个控件设定一个唯一的标识符(ID),以便后续操作能够准确调用并结合上下文信息进行处理

复制代码
    android:id="@+id/名字"

设置相对位置

复制代码
 android:layout_below     //在某元素的下方

    
 android:layout_above     //在某元素的的上方
    
 android:layout_toLeftOf     //在某元素的左边
    
 android:layout_toRightOf     //在某元素的右边
    
 android:layout_alignTop     //本元素的上边缘和某元素的的上边缘对齐
    
 android:layout_alignLeft     //本元素的左边缘和某元素的的左边缘对齐
    
 android:layout_alignBottom     //本元素的下边缘和某元素的的下边缘对齐
    
 android:layout_alignRight     //本元素的右边缘和某元素的的右边缘对齐

在初步学习过程中熟悉并尝试运用这两种关键组件搭建模拟计算器时发现虽然能够完成基本框架搭建但未能充分挖掘线性排列与相对定位技术的独特优势最终呈现出的效果不尽如人意其中最为显著的问题在于过度依赖相对定位技术却未能做到精准应用反而导致所设计的功能界面品质极差此外在适应不同场景的能力方面也存在明显不足

这次学习活动不仅有效地解决了困扰我提出的问题,并且对原有的布局进行了优化工作。

二、布局的优化技巧

1、比例划分

前提是 线性布局,内部元素可以按照比例划分

需要设置权重:

复制代码
    android:layout_weight=" "

纵向布局仅能分配各内部控件的高度
横向布局仅能分配各内部控件的宽度
建议将需要按照比例分配的宽度或高度设置为0dp
2. 局部嵌套结构

任何一种结构都可以被视为一个整体,并且这个整体也可以成为另一个布局的一部分

基于模拟手机计算器布局的案例分析, 我认为采用了线性布局的比例划分和布局嵌套进行优化

思路:主要采取纵向线性布局为主,并通过合理设置分隔比例来优化适应性。在这些布局中增添了一些采取分隔比例划分的方式。

不足是没有使用相对布局

代码如下:

复制代码
 <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="com.example.hello.MainActivity$PlaceholderFragment" >
    
  
    
     <TextView
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="2"
    
     android:background="#808080" />
    
  
    
     <TextView
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:background="#A9A9A9"
    
     android:gravity="bottom|right"
    
     android:textSize="25sp"
    
     android:text="2016" />
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:background="#D4F2E7"
    
     android:orientation="horizontal" >
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="C" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="Back" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="/" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="*" />
    
     </LinearLayout>
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:background="#D4F2E7"
    
     android:orientation="horizontal" >
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="1" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="2" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="3" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="+" />
    
     </LinearLayout>
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:background="#D4F2E7"
    
     android:orientation="horizontal" >
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="4" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="5" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="6" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="-" />
    
     </LinearLayout>
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:background="#D4F2E7"
    
     android:orientation="horizontal" >
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="7" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="8" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="9" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="=" />
    
     </LinearLayout>
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:background="#D4F2E7"
    
     android:orientation="horizontal" >
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="3"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="0" />
    
  
    
     <TextView
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:layout_gravity="center"
    
         android:textSize="25sp"
    
         android:text="·" />
    
     </LinearLayout>
    
  
    
 </LinearLayout>

效果图展示:

在对原有布局进行优化的过程中,在对原有布局进行优化的过程中,在对原有布局进行优化的过程中,在对原有布局进行优化的过程中,在对原有布局进行优化的过程中,在对原有布局进行优化的过程中,在对原有布局进行优化的过程中,在对原有布局进行优化的过程中,在对原有布局进行优化的过程中

(值得注意的是,在嵌套层次过多的情况下)

(在这种情况下)

导致维护和管理上的不便

而这些问题可能会降低系统的效率

因此在实际应用中应当避免过度使用 layout nesting

复制代码
 <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"
    
     android:paddingBottom="@dimen/activity_vertical_margin"
    
     android:paddingLeft="@dimen/activity_horizontal_margin"
    
     android:paddingRight="@dimen/activity_horizontal_margin"
    
     android:paddingTop="@dimen/activity_vertical_margin"
    
     tools:context="com.example.calculator616.MainActivity$PlaceholderFragment" >
    
  
    
     <TextView
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:background="#778899" />
    
  
    
     <TextView
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="2"
    
     android:background="#E6E6FA"
    
     android:gravity="bottom|right"
    
     android:text="2016"
    
     android:textColor="#0000FF"
    
     android:textSize="18sp" />
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:orientation="horizontal" >
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="C"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="Back"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="/"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="*"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
     </LinearLayout>
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:orientation="horizontal" >
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="1"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="2"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="3"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="+"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
     </LinearLayout>
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="1"
    
     android:orientation="horizontal" >
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="4"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="5"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="6"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="-"
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
     </LinearLayout>
    
  
    
     <LinearLayout
    
     android:layout_width="match_parent"
    
     android:layout_height="0dp"
    
     android:layout_weight="2"
    
     android:orientation="horizontal" >
    
  
    
     <LinearLayout
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="3"
    
         android:orientation="vertical" >
    
         <LinearLayout 
    
             android:layout_width="match_parent"
    
             android:layout_height="0dp"
    
             android:layout_weight="1"
    
             android:orientation="horizontal">
    
             <Button 
    
                 android:layout_width="0dp"
    
                 android:layout_height="match_parent"
    
                 android:layout_weight="1"
    
                 android:text="7"
    
                 android:textColor="#00008B"
    
                 android:textSize="15sp" />
    
             <Button 
    
                 android:layout_width="0dp"
    
                 android:layout_height="match_parent"
    
                 android:layout_weight="1"
    
                 android:text="8"
    
                 android:textColor="#00008B"
    
                 android:textSize="15sp" />
    
             <Button 
    
                 android:layout_width="0dp"
    
                 android:layout_height="match_parent"
    
                 android:layout_weight="1"
    
                 android:text="9"
    
                 android:textColor="#00008B"
    
                 android:textSize="15sp" />
    
         </LinearLayout>
    
         <LinearLayout 
    
             android:layout_width="match_parent"
    
             android:layout_height="0dp"
    
             android:layout_weight="1"
    
             android:orientation="horizontal">
    
             <Button 
    
                 android:layout_width="0dp"
    
                 android:layout_height="match_parent"
    
                 android:layout_weight="2"
    
                 android:text="0"
    
                 android:textColor="#00008B"
    
                 android:textSize="15sp"/>
    
             <Button 
    
                 android:layout_width="0dp"
    
                 android:layout_height="match_parent"
    
                 android:layout_weight="1"
    
                 android:text="·"
    
                 android:textColor="#00008B"
    
                 android:textSize="15sp"/>
    
             </LinearLayout>
    
     </LinearLayout>
    
  
    
     <Button
    
         android:layout_width="0dp"
    
         android:layout_height="match_parent"
    
         android:layout_weight="1"
    
         android:text="="
    
         android:textColor="#00008B"
    
         android:textSize="15sp" />
    
     </LinearLayout>
    
  
    
 </LinearLayout>

效果图展示:

全部评论 (0)

还没有任何评论哟~