程序员

四大layout

2018-06-26  本文已影响0人  _iamjerry

LinerLayout 线性布局

LinerLayout, 中文名为线性布局。这个布局会将它所包含的控件在线性方向上依次排列。

我们可以通过android:orientation属性来指定排列方向。

vertical为垂直方向,horizontal为水平方向

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="button 1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 3"
        />

运行效果:

LINERLAYOUT2.png

注意:如果是vertical垂直方向,则内部的控件不能将android:layout_height指定为match_parent,因为这样的话,单独的一个控件就已经把整个垂直方向占据了,接下来的控件就没有可以放置的位置了,而导致其它的控件无法显示。同理,如果是horizontal水平方向,则内部的控件不能将android:layout_width指定为match_parent

android:layout_gravity用于指定控件在布局中的对齐方式。

vertical垂直方向,只有水平方向上的对齐方式才会生效。

horizontal水平方向,只有垂直方向上的对齐方式才会生效。

<Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="button 1"
       android:layout_gravity="top"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 2"
        android:layout_gravity="center_vertical"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 3"
        android:layout_gravity="bottom"/>

运行效果:

LINERLAYOUT1.png

android:layout_weight 可以用比例的方式来指定控件的大小,其在手机屏幕的适配上起到很重要的作用。

   <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:hint="Type something"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="send"/>

运行效果:

LINERLAYOUT3.png

RelativeLayout 相对布局

RelativeLayout又称为相对布局,通过相对定位的方式让控件出现在布局的任何地方。

relativelayout1.png

父容器定位属性示意图

relativelayout2.png

图片来自runoob

<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:id="@+id/button1"
        android:text="button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="button2"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="button3"
        android:layout_centerInParent="true"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:text="button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:text="button5"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

运行效果:

relativelayout3.png

以上是相对于父布局定位的。Button1和父布局的左上角对齐,Button2和父布局的右上角对齐,Button3居中显示,Button4和父布局的左下角对齐,Button5和父布局的左下角对齐。

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="button3"
        android:layout_centerInParent="true"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="button1"
        android:layout_above="@id/button3"
        android:layout_toLeftOf="@id/button3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="button2"
        android:layout_above="@id/button3"
        android:layout_toRightOf="@id/button3"/>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:text="button4"
        android:layout_below="@id/button3"
        android:layout_toLeftOf="@id/button3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:text="button5"
        android:layout_below="@id/button3"
        android:layout_toRightOf="@id/button3"/>

运行效果:

relativelayout4.png

以上是每个控件都是以Button3 控件进行定位的。

FrameLayout 帧布局

FrameLayout又称为帧布局,所有的控件都会默认摆放在布局的左上角。

<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:text="This is TextView"/>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageview"
    android:src="@mipmap/ic_launcher"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button"/>

</FrameLayout>

运行效果:

framelayout1.png

所有的控件都位于布局的左上角,而且按照顺序叠在一起。

我们可以通过android:layout_gravity去指定控件在布局中的对齐方式。

Percent-support-lib 百分比布局

只有LinearLayout支持使用layout_weight属性来实现按比例指定控件大小的功能,其他的布局并不支持这属性。因此,Android引入了一种全新的布局方式来解决这个问题-----百分比布局。可以直接指定控件在布局中所占的百分比。

百分比布局为FrameLayoutRelativeLayout进行了功能扩展,提供了PercentFrameLayoutPercentRelativeLayout两个全新的布局。

build.gradle添加百分比布局的依赖。

打开app/build.gradle,在dependencies闭包添加以下内容:

implementation 'com.android.support:percent:25.3.0'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.0'
    implementation 'com.android.support:percent:25.3.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

PercentRelativeLayout

修改xml文件

<android.support.percent.PercentRelativeLayout
    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">
    
    <View
        android:id="@+id/top_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:background="#ff44aacc"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%" />

    <View
        android:id="@+id/top_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/top_left"
        android:background="#ffe40000"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="30%" />

    <View
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/top_left"
        android:background="#ff00ff22"
        app:layout_heightPercent="80%" />
</android.support.percent.PercentRelativeLayout>

运行效果:

percentrelativelayout.png

可以看到通过app:layout_heightPercentapp:layout_widthPercent两个参数进行百分比设定。

PercentFrameLayout

<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:id="@+id/button1"
        android:text="Button1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:id="@+id/button2"
        android:layout_gravity="right|top"
        android:text="Button2"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%" />
    <Button
        android:id="@+id/button3"
        android:text="Button3"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:id="@+id/button4"
        android:text="Button4"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

</android.support.percent.PercentFrameLayout>

运行效果:

percentframelayout.png

其它的属性还有

可以参考android-percent-support-lib-sampleAndroid 百分比布局库(percent-support-lib) 解析与扩展

上一篇下一篇

猜你喜欢

热点阅读