UI布局初试three---四种基本布局

wqbendy 7年前
   <ul>     <li>版权声明:欢迎转载,转载请注明出处。 如果本文帮助到你,本人不胜荣幸,如果浪费了你的时间,本人深感抱歉。如果有什么错误,请一定指出,以免误导大家、也误导我。感谢关注。</li>    </ul>    <h2>什么是布局?</h2>    <p>布局是一种可以放置很多控件的容器,他可以按照一定的规律调整内部控件的位置,从而编写出好看的界面。布局的内部也能够放置布局,形成多层嵌套。</p>    <h3>线性布局 LinearLayout</h3>    <p>LinearLayout 叫做线性布局,他会将其内部的控件在线性的方向依次排列。 他是按照属性android:orientation的指定,来决定其子视图设置为水平还是垂直。</p>    <ul>     <li>horizontal 表示一行 (水平) 默认的值</li>     <li>vertical 表示一列 (垂直)</li>    </ul>    <p>LinearLayout 是布局中的根视图,因此应将宽度和高度设置为 "match_parent",从而填满可供应用使用的整个屏幕区域。</p>    <pre>  <code class="language-java">栗子:      <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">            <Button              android:layout_gravity="top"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="Button 1" />            <Button              android:layout_gravity="bottom"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="Button 2"/>      </LinearLayout></code></pre>    <p>android:layout_gravity属性:指定控件在布局中对齐方式,需要根据父视图的排列方式来指定,比如父视图对齐方式为vertical,只有在水平方向上的对齐方式才有效</p>    <p>然后就是LinearLayout的重要属性---android:layout_weight:一般称为权重。他是使用比例的方式来指定控件的大小。</p>    <pre>  <code class="language-java">栗子:      <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="horizontal">            <EditText              android:layout_width="0dp"              android:layout_height="wrap_content"              android:layout_weight="1"              android:hint="Input someing"/>            <Button              android:layout_width="0dp"              android:layout_weight="1"              android:layout_height="wrap_content"              android:text="Button"/>      </LinearLayout></code></pre>    <p>首先把width的值指定为0,可以理解成宽度不再受width控制,而由weight来控制,系统会把所有的weight的值加起来,然后按份分配给各个控件。另外,我们也可以只指定其中一部分控件的weight值来实现UI效果。</p>    <h3>相对布局 RelativeLayout</h3>    <p>他可以通过相对定位的方式让控件出现在布局的任何位置。可以相对与父视图定位,也可以相对于控件定位</p>    <pre>  <code class="language-java">栗子:      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"          android:layout_width="match_parent"          android:layout_height="match_parent">          <Button              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_alignParentLeft="true"              android:layout_alignParentTop="true"              android:text="Button 1" />          <Button              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_alignParentRight="true"              android:layout_alignParentTop="true"              android:text="Button2 " />          <Button              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_centerInParent="true"              android:text="Button 3" />        </RelativeLayout></code></pre>    <p>android:layout_alignParentLeft:相对于父控件左对齐,其他的依次类推</p>    <p>android:layout_centerInParent:与父控件的中心对齐</p>    <pre>  <code class="language-java">栗子:      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"          android:layout_width="match_parent"          android:layout_height="match_parent">            <Button              android:id="@+id/btn3"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_centerInParent="true"              android:text="Button 3"/>            <Button              android:id="@+id/btn1"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_above="@id/btn3"              android:layout_toLeftOf="@id/btn3"              android:text="Button 1" />            <Button              android:id="@+id/btn2"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_above="@id/btn3"              android:layout_toRightOf="@id/btn3"              android:text="Button 2"/>          <Button              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_alignLeft="@id/btn1"              android:text="Button 4"/>      </RelativeLayout></code></pre>    <p>android:layout_above:让一个控件位于其指定控件的上方,需要指定相对控件的id,其他的依次类推</p>    <p>android:layout_toLeftOf:让一个控件位于其指定控件的左侧</p>    <p>android:layout_alignLeft:让一个控件的左边缘与另外一个控件的左边缘对齐</p>    <p>这里我们需要注意的是:如果一个控件要引用另外一个控件的id时,该控件一定要定义在引用控件的后面,不然会找不到id。</p>    <h3>帧布局 FrameLayout</h3>    <p>FrameLayout是最简单的布局了。应用场景相对于前面两种来说也少了很多。 所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。</p>    <pre>  <code class="language-java">栗子:      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"          android:layout_width="match_parent"          android:layout_height="match_parent">            <TextView              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:textSize="50sp"              android:textColor="#ff0000"              android:text="Text"/>            <Button              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="Button" />      </FrameLayout></code></pre>    <p>简而言之,他根本控制不了子控件在其内部的位置</p>    <h3>百分比布局 Percent</h3>    <p>可以不用match_parent或者wrap_content等方式来指定控件的大小, 而是直接指定控件在布局中所占的百分比,可以轻松实现按任意比例分配布局的效果。</p>    <p>LinearLayout已经可以使用权重来按比例分配控件大小了,所以只需要为FrameLayout和RelativeLayout提供扩展。</p>    <ul>     <li>PercentFrameLayout 继承了FrameLayout的特性</li>     <li>PercentRelativeLayout 继承了RelativeLayout的特性</li>    </ul>    <p>百分比布局属于新增的布局,我们要想使用他,就需要在项目下的build.gradle 文件添加百分比布局库的依赖。具体就是在dependencies闭包中添加以下内容:</p>    <p>compile 'com.android.support:percent:24.2.1'</p>    <pre>  <code class="language-java">栗子:      <android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"          xmlns:app="http://schemas.android.com/apk/res-auto"          android:layout_width="match_parent"          android:layout_height="match_parent">            <Button              android:layout_gravity="left|top"              android:text="Button 1"              app:layout_heightPercent="50%"              app:layout_widthPercent="50%" />            <Button              android:layout_gravity="right|top"              android:text="Button 2"              app:layout_heightPercent="50%"              app:layout_widthPercent="50%" />            <Button              android:layout_gravity="left|bottom"              android:text="Button 3"              app:layout_heightPercent="50%"              app:layout_widthPercent="50%" />            <Button              android:layout_gravity="right|bottom"              android:text="Button 4"              app:layout_heightPercent="50%"              app:layout_widthPercent="50%" />        </android.support.percent.PercentFrameLayout></code></pre>    <p>由于PercentFrameLayout不是系统内置的布局,所以我们需要写出完整的路径,并且给他定义一个app的命名空间。所以我们可以使用app:layout_widthPercent与app:layout_heightPercent来指定控件的宽与高</p>    <p>好了,对于四种基本布局的基本用法暂时就了解到这里。慢慢用到新的知识再来分享。</p>    <p> </p>    <p>来自:http://www.jianshu.com/p/1bc5383a5e91</p>    <p> </p>