安卓开发学习笔记(⼋):线性布局
线性布局是Android中较为常⽤的布局⽅式,使⽤LinearLayout标签。线性布局主要有两种形式,⼀种是⽔平线性布局,⼀种是垂直线性布局。需要注意的是Android的线性布局不会换⾏,当组件⼀个挨着⼀个地排列到头之后,剩下的组件将不会被显⽰出来。
下表显⽰了LinearLayout⽀持的常⽤XML属性及相关⽅法的说明。
LinearLayout 包含的所有⼦元素都受 LinearLayout.LayoutParams 控制,因此 LinearLayout包含的⼦元素可以额外指定如如下属性。
android:layout_gravity:指定该⼦元素在LinearLayout中的对齐⽅式。
android:layout_weight:指定该⼦元素在LinearLayout中所占的权重。下⾯我们来看看具体如何进⾏线性布局:
⼀.线性布局的控件在整个控件最⾼处
<LinearLayout xmlns:android="schemas.android/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="schemas.android/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="schemas.android/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布局
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"//这⾥⽤这个将textview写到了整个线性布局的最下⾯ android:layout_marginLeft="10dp"/>
</LinearLayout>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论