布局相关的知识点

来源:互联网 发布:网游推荐知乎 编辑:程序博客网 时间:2024/06/05 08:20

1、

 android:gravity="center"//一般指文字在控件中的对齐方式 android:layout_gravity="right" //一般指控件在布局中的对齐方式

2、使用android:layout_weight去分配控件占用的空间时,规范写法试讲相应的尺寸设置为0,通过android:layout_weight来设置各元素所占用的比例。各个控件的android:layout_weight占个控件总的android:layout_weight和的比例。

<LinearLayout        android:layout_height="wrap_content"        android:layout_width="match_parent"        android:orientation="horizontal"        >        <EditText            android:id="@+id/et1"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:hint="type something here"            android:maxLines="2"            />        <Button            android:layout_height="wrap_content"            android:layout_width="wrap_content"            android:text="确定"            />    </LinearLayout>

3、相对布局
(1)

<?xml version="1.0" encoding="utf-8"?><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.example.david.firstcode_ui.Main2Activity">    <Button        android:id="@+id/bt1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_above="@id/bt5"        android:layout_toLeftOf="@id/bt5"        />    <Button        android:id="@+id/bt2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button2"        android:layout_above="@id/bt5"        android:layout_toRightOf="@id/bt5"        />    <Button        android:id="@+id/bt3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_below="@id/bt5"        android:layout_toLeftOf="@id/bt5"        />    <Button        android:id="@+id/bt4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_below="@id/bt5"        android:layout_toRightOf="@id/bt5"        />    <Button        android:id="@+id/bt5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_centerInParent="true"        /></RelativeLayout>

这里写图片描述

(2)

<?xml version="1.0" encoding="utf-8"?><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.example.david.firstcode_ui.Main2Activity"><Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="button1"    android:layout_alignParentLeft="true"    android:layout_alignParentTop="true"    />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button2"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_alignParentLeft="true"        android:layout_alignParentBottom="true"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_alignParentRight="true"        android:layout_alignParentBottom="true"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_centerInParent="true"        /></RelativeLayout>

这里写图片描述

原创粉丝点击