达内课程-布局控件之相对布局和线性布局

来源:互联网 发布:网易域名邮箱注册 编辑:程序博客网 时间:2024/06/09 19:47

相对布局RelativeLayout
线性布局LinearLayout
表格布局TableLayout
帧布局FrameLayout
网格布局GridLayout

RelativeLayout

停靠父控件边界
alignParentTop
alignParentBottom
alignParentLeft
alignParentRight

相对父控件居中
centerInParent
centerHorizontal
centerVertical

停靠周围控件边界
above
below
toRightOf
toLeftOf

与周围控件边界对齐
alignTop
alignBottom
alignLeft
alignRight
alignBaseLine

练习

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.xy.csdnproject.RelativeLayoutActivity">    <!--        @+id新定义id        在R类中新定义一个整数常量        如果常量已存在就引用现有常量        @id引用id        在引用id时也可以使用@+id        但是如果写错的已存在的id就会新创建一个整数常量        所以为了严谨应该用@id    -->    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn1"/>    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:text="btn2"/>    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_alignParentRight="true"        android:text="btn3"/>    <Button        android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_centerVertical="true"        android:text="btn4"/>    <Button        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="btn5"/>    <Button        android:id="@+id/button6"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:text="btn6"/>    <Button        android:id="@+id/button7"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:text="btn7"/>    <Button        android:id="@+id/button8"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:text="btn8"/>    <Button        android:id="@+id/button9"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="btn9"/>    <Button        android:id="@+id/button10"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/button1"        android:layout_toRightOf="@id/button1"        android:layout_above="@id/button4"        android:layout_alignRight="@id/button2"        android:text="btn10"/>    <Button        android:id="@+id/button11"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@id/button4"        android:layout_toRightOf="@id/button4"        android:layout_toLeftOf="@id/button5"        android:layout_above="@id/button7"        android:text="btn11"/>    <Button        android:id="@+id/button12"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@id/button11"        android:layout_toRightOf="@id/button11"        android:layout_toLeftOf="@id/button6"        android:text="btn12"/></RelativeLayout>

LinearLayout

布局方向
orientation=”horizontal” 水平(默认)
orientation=”vertical” 垂直

比重,比例
1、按比例来分配剩余可用空间
layout_weight=”1”
layout_weight=”2”
2、如果让控件大小严格按照比例显示,可以把高度或宽度设置为“0dp”

重力
layout_gravity=”可选值”
水平布局中可选值
top
bottom
center_vertical

垂直布局中可选值
left
right
center_horizontal

center

练习

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.xy.csdnproject.LinearLayoutActivity"    android:orientation="vertical"    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn1"        android:layout_weight="2"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn2"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn3"        android:layout_weight="1"/></LinearLayout>

这样设置,btn1和btn2的总高度不是2:1,因为按钮原来有一个高度,所谓的2:1是剩余高度比例

如果想让btn1和btn2的总高度比例是2:1应把高度设置为0dp

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.xy.csdnproject.LinearLayoutActivity"    android:orientation="vertical"    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn1"        android:layout_weight="2"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn2"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn3"        android:layout_weight="1"/></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.xy.csdnproject.LinearLayoutActivity"    android:orientation="vertical"    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn1"        android:layout_weight="2"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="2"        android:background="#ccc">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="btn4"            android:layout_gravity="center_vertical"/>        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:text="btn5"            android:layout_weight="1"            android:layout_gravity="bottom"/>    </LinearLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn2"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn3"        android:layout_weight="1"/></LinearLayout>
阅读全文
0 0
原创粉丝点击