安卓开发学习笔记(八):线性布局

线性布局是Android中较为常用的布局方式,使用LinearLayout标签。线性布局主要有两种形式,一种是水平线性布局,一种是垂直线性布局。需要注意的是Android的线性布局不会换行,当组件一个挨着一个地排列到头之后,剩下的组件将不会被显示出来。

下表显示了LinearLayout支持的常用XML属性及相关方法的说明。

LinearLayout 包含的所有子元素都受 LinearLayout.LayoutParams 控制,因此 LinearLayout包含的子元素可以额外指定如如下属性。

android:layout_gravity:指定该子元素在LinearLayout中的对齐方式。

android:layout_weight:指定该子元素在LinearLayout中所占的权重。下面我们来看看具体如何进行线性布局:

一.线性布局的控件在整个控件最高处

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
//这里用这个将textview写到了整个线性布局的最下面上方
 android:layout_marginLeft="10dp"/> </LinearLayout>

二.线性布局的控件在整个控件的中间

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
//这里用这个将textview写到了整个线性布局的最中间
 android:layout_marginLeft="10dp"/> </LinearLayout>

三.线性布局的控件在整个控件的最下方

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"//这里用这个将textview写到了整个线性布局的最下面
        android:layout_marginLeft="10dp"/>


</LinearLayout>

猜你喜欢

转载自www.cnblogs.com/geeksongs/p/10455890.html