Android 学习第14课,Android 布局

时间:2021-05-27 07:19:41

布局分4种:

1. LinearLayout (线性布局)

file:///H:/tool/01/Android/android-sdk-windows/docs/guide/topics/ui/layout/linear.html

事例代码:

Android 学习第14课,Android 布局

2. RelativeLayout (相对布局)

Android 学习第14课,Android 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
> <TextView
android:id="@+id/numbeLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/number" /> <EditText
android:id="@+id/number2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@null"
android:layout_below="@id/numbeLabel" //这里表示这个控件显示在id为numberLabel这个控件下面
/> <Button
android:id="@+id/ok"
android:text="@string/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/number2"
android:layout_alignParentRight="true" //这里表示这个控件在父对象里面是居右显示
android:layout_marginLeft="10px" //这里表示这个控件跟左边这个控制的间距是10像素
/> <Button
android:text="@string/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok" //表示显示在ok这个按钮左边
android:layout_alignTop="@id/ok" //表示与ok这个按钮,顶对齐
/> </RelativeLayout>

3. TableLayout (表格布局)

Android 学习第14课,Android 布局

4. FrameLayout (帧布局)

就像gif图片一样,或者可以理解成层,可以叠加在一起

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/bofangtwo"
android:contentDescription="@string/app_name"
/> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bofang"
android:contentDescription="@string/app_name"
android:layout_gravity="center"
/>
</FrameLayout>

注意:在写

android:src="@drawable/bofang"的时候,注意图片后面的扩展名不要带,带上就会出错!
把bofang.png和bofangtwo.jpg复制到res中的drawable-hdpi中

Android 学习第14课,Android 布局