android开发之布局小技巧

本来星期五就应该把这个记下来了.可惜玩了两三局游戏就太晚了.直到现在才有心把这个记录下来.

星期五布了一个控件很多的界面,想了一下,最后决定全部用线性布局来实现.

线性布局嵌套线性布局.

比较麻烦的是各个控件的大小比较难调.

对于这种情况,就有一个小技巧.使用weight属性.

比如,在横向布局中将控件的宽度设置为0

再设置weight属性,就可以得到比较好的效果

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:orientation="vertical" >
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical" >
这样就可以轻易的在一个横向的线性布局中划分出两个大小为5:2的线性布局框.

同理,如果在纵向线性布局中设置控件大小,就应该将高度设置为0,然后再设置weight属性..........

附上布局图片以及代码


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="接收区:"
                android:textSize="20sp" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="回显" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:text="十六进制" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="停止显示" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="自动清空" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="80dp"
                android:layout_weight="0.47"
                android:text="清空" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:ems="10" >

            <requestFocus />
        </EditText>

		<LinearLayout
		    android:layout_width="fill_parent"
		    android:layout_height="wrap_content" >

		    <TextView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:text="发送区:"
		        android:textSize="20sp" />

		    <CheckBox
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:layout_marginLeft="80dip"
		        android:text="十六进制" />

		    <CheckBox
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:layout_marginLeft="20dp"
		        android:text="自动换行" />

		    <CheckBox
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:layout_marginLeft="20dp"
		        android:text="自动清空" />

		    <Button
		        android:layout_width="match_parent"
		        android:layout_height="wrap_content"
		        android:layout_marginLeft="100dp"
		        android:text="清空" />

		</LinearLayout>

		<EditText
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    android:layout_weight="2"
		    android:ems="10" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="串口名:"
                android:textSize="20sp" />

            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="波特率:"
                android:textSize="20sp" />

            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3" />

        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="数据位:"
                android:textSize="20sp" />

            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3" />

        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="校验位:"
                android:textSize="20sp" />

            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3" />

        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="停止位:"
                android:textSize="20sp" />

            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3" />

        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3" />

            <Switch
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="自动发送" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ms" />

        </LinearLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
        
    </LinearLayout>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值