当前位置:操作系统 > 安卓/Android >>

android:layout_weight和android:layout_gravity用法

android:layout_weight用来分配更多的空间给该控件
android:layout_gravity是用来设置该button相对与父view的位置
带layout的都是相对于父控件而言.

 

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#D1EEEE" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" > 
 
    <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:hint="to" /> 
 
    <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:hint="subject" /> 
 
<SPAN style="COLOR: #ff9966">    //android:layout_weight用来分配更多的空间给该控件</SPAN> 
    <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:gravity="top" 
        android:hint="message" /> 
     
<SPAN style="COLOR: #ff9966">    //默认位置是距做对齐</SPAN> 
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Defaultsend" /> 
     
  <SPAN style="COLOR: #ff9900">  //android:layout_gravity是用来设置该button相对与父view的位置</SPAN> 
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="right" 
        android:text="send" /> 
 
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#D1EEEE"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="to" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="subject" />

    //android:layout_weight用来分配更多的空间给该控件
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="message" />
   
    //默认位置是距做对齐
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Defaultsend" />
   
    //android:layout_gravity是用来设置该button相对与父view的位置
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="send" />

</LinearLayout>

 

\

 

 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,