Android下的布局

本文详细介绍了在Android中如何通过属性控制布局和窗体的位置关系,包括使用`layout_width`、`layout_height`、`layout_marginLeft`、`paddingLeft`等属性实现控件的精确定位。此外,还探讨了`layout_weight`属性在宽度分配中的作用,以及当`layout_width=“fill_parent”`时,控件宽度显示的比例计算方法。通过实例解析,帮助开发者更好地理解和运用这些布局技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

控件的相对位置

需求:在屏幕上写一个十字型的button

一:通过属性控制布局和窗体的位置关系(前提:layout_width和layout_height 至少有一个为warp_content

代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"//这里可以设置为:center left right等表现为①②③
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="十字型button" />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        >
       <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />        
    </LinearLayout>    
	<LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
        <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
        <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
	</LinearLayout>
	<LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
	    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
	</LinearLayout>
</LinearLayout>
//具体表现如下①②③


二 通过设定值确定与边框的位置关系

android:layout_marginLeft指该控件距离边父控件的边距,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="30dip"//用数值方法设置与边缘的位置关系
    android:layout_marginTop="70dip"
    
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="十字型button" />

控件中元素的位置

android:paddingLeft指该控件内部内容,如文本距离该控件的边距。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="控件的相对位置" />

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        >
       <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4" />   

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

        <Button 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_marginLeft="1dip"
	        android:text="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="277px"
            android:text="2"/>

	</LinearLayout>

	<LinearLayout
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content" 
	    android:layout_gravity="center_horizontal"
	    >

		<Button
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:gravity="left"
		    android:text="3" />

	</LinearLayout>

</LinearLayout>


控件中内容的相对位置2

below 属性的使用

 <RelativeLayout 
        	android:layout_width="fill_parent"
	        android:layout_height="wrap_content">
        <Button 
            android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
            android:text="12"/>
        <Button 
            android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
            android:text="13"
            android:layout_alignParentRight = "true" />
        <Button 
            android:id="@+id/bt5"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
            android:text="14"/>
        <Button 
            android:layout_below="@id/bt5"
            android:layout_centerHorizontal="true"//如果没有设置居中属性的话控件会放在左边
            android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
            android:text="15"/>     
    </RelativeLayout>

paddingLeft和marginLeft

android:paddingLeft="80px"
按钮上设置的内容(例如图片)离按钮左边边界80个像素
android:layout_marginLeft="80px"
整个按钮离左边设置的内容80个像素
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <EditText 
            android:layout_marginLeft="80dip"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:text="请输入姓名"
        	android:background="#ff0000"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <EditText 
            android:paddingLeft="80dip"
            android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:text="请输入电话"
        	android:background="#ff0000"        	
            />
    </LinearLayout>


layout_weight

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout 
	        android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    android:orientation="horizontal">
        <TextView 
            android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
            android:text="1"
            android:layout_weight="1" 
            android:background="#ffccbb"
            />
        <TextView 
            android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="2" 
            android:background="#ff0000"
            />
        <TextView 
            android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
            android:text="3"
            android:layout_weight="3" 
            android:background="#ffeeee"
            />
        
    </LinearLayout>
    <LinearLayout 
	        android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:orientation="horizontal">
        <TextView 
            android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
            android:text="4"
            android:layout_weight="1" 
            android:background="#ffccbb"
            />
        <TextView 
            android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
            android:text="5"
            android:layout_weight="2" 
            android:background="#ff0000"
            />
        <TextView 
            android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
            android:text="6"
            android:layout_weight="3" 
            android:background="#ffeeee"
            />
        
    </LinearLayout>
	<LinearLayout 
	        android:layout_width="fill_parent"
		    android:layout_height="wrap_content"
		    android:orientation="horizontal">
        <TextView 
            android:layout_width="fill_parent"
	    	android:layout_height="wrap_content"
            android:text="7"
            android:layout_weight="1" 
            android:gravity="center_horizontal"
            android:background="#ffccbb"
            />
        <TextView 
            android:layout_width="fill_parent"
	    	android:layout_height="wrap_content"
            android:text="8"
            android:layout_weight="2" 
            android:gravity="left"
            android:background="#ff0000"
            />
        <TextView 
            android:layout_width="fill_parent"
	    	android:layout_height="wrap_content"
            android:text="9"
            android:layout_weight="3" 
            android:gravity="left"
            android:background="#ffeeee"
            />
    </LinearLayout>
</LinearLayout>
三个文本框的都是 layout_width=wrap_content 时,width显示的比例为1:2:3

layout_width=“fill_parent时,width显示的比例是:

系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件

那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )

那么第7个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/6 * 剩余空间大小(-2 parent_width)=2/3parent_width

同理第8个TextView的实际所占宽度=parent_width + 2/6*(-2parent_width)=1/3parent_width;

第9个TextView的实际所占宽度=parent_width + 3/6*(-2parent_width)=0parent_width;所以就是2:1:0的比列显示了。第三个就直接没有空间了。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值