Android 中的四种基本布局

来源:互联网 发布:君将哀而生之乎的翻译 编辑:程序博客网 时间:2024/05/17 10:53
LinearLayout
LinearLayout 又称作线性布局,是一种非常常用的布局。正如它的名字所秒速的一样,这个布局会将它所包含的控件在线性方向上依次排列。

这里注意的是:如果LinearLayout的排列方向是horizontal,内部的控件就绝对不能将宽度指定为match_parent,因为这样的话单独一个控件就会将整个水平方向占满,其他的控件就没有可放置的位置了。同样的道理,如果LinearLayout的排列方向是Vertical,内部的控件就不能将高度指定为match_parent.


android:gravity是用于指定文字在控件中的对齐方式,而android:layout_gravity是用于指定控件在布局中的对齐方式
android:layout_gravity的可选值和android:gravity差不多
当LinearLayout的排列方向是horizontal时,只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。同样的道理,当LinearLayout的排列方向是Vertical时,只有水平方向的对齐方式才会生效。


android:layout_weight:

允许我们使用比例的方式来指定控件的大小,它在手机屏幕的适配性方面可以起到非常重要
的作用。


RelativeLayout
相对布局

相对于父布局定位:
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"

相对于一个控件定位:
android:layout_above:可以让一个控件位于另一个控件的上方
android:layout_below:可以让一个控件位于另一个控件的下方
android:layout_toLeftOf:可以让一个控件位于另一个控件的左侧
android:layout_toRightOf:可以让一个控件位于另一个控件的右侧


注意,当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后
面,不然会出现找不到id的情况。


另外一组相对于控件进行定位的属性:
android:layout_alignLeft:表示让一个控件的左边缘和另一个控件的左边缘对齐
android:layout_alignRight:表示让一个控件的右边缘和另一个控件的右边缘对齐
android:layout_alignTop:表示让一个控件的顶部边缘和另一个控件的顶部边缘对齐
android:layout_alignBottom:表示让一个控件的底部边缘和另一个控件的底部边缘对齐


FrameLayout
FrameLayout没有任何的定位方式,所有的控件都会摆放在布局的左上角。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</FrameLayout>





TableLayout
这种布局不是很常用。
activity_main.xml中的代码改成如下所示:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >119
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your account" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"                            //将两列合并成一列
android:text="Login" />
</TableRow>
</TableLayout>


注意:每加入一个TableRow就表示在表格中添加了一行,然后在TableRow中每加入一个控件,就表示在该行中加入了一列,TableRow中的控件是不能指定宽度的


解决方法:android:stretchColumns




自定义控件
控件的布局结构




所用的所有控件都是直接或间接继承自View。
所有布局都是直接或间接继承自ViewGroup。


ViewAndroid中一种最基本的 UI 组件,它可以在屏幕
上绘制一块矩形区域,并能响应这块区域的各种事件,因此,我们使用的各种控件其实就是View的基础之上又添加了各自特有的功能

ViewGroup则是一种特殊的View,它可以包含很多的子View 和子ViewGroup,是一个用于放置控件和布局的容器。


android:layout_margin:这个属性,可以指定控件在上下左右方向上偏移的距离。
android:layout_marginLeft或android:layout_marginTop等属性来单独指定控件在某个方向上的距离


例子1: 创建带有返回键和编辑键的标题栏
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg" >
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/back_bg"
android:text="Back"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp" />
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>


然后在每一个布局添加标题栏:
<include layout="@layout/title" />


创建自定义控件:
1.新建TitleLayout 继承LinearLayout
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
}
}

然后我们需要在布局文件中添加这个自定义控件,修改
activity_main.xml中的代码,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.uicustomviews.TitleLayout>
</LinearLayout>
0 0
原创粉丝点击