`
yxwang0615
  • 浏览: 550391 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

android:layout_weight

 
阅读更多

父视图剩余的空间分配给设 置了layout_weight的组件。这个属性可以让LinearLayout里不同的组件分配不同宽度/高度变得非常灵活。默认的 android:layout_weight = 0,即缺省。layout_weight的值越大,所占比例也越大。

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
     android:gravity="center_horizontal"
    android:text="red"
     android:layout_weight="1"
    android:background="#aa0000"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
     android:gravity="center_horizontal"
    android:text="green"
     android:layout_weight="1"
    android:background="#00aa00"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
     android:gravity="center_horizontal"
    android:text="blue"
     android:layout_weight="1"
    android:background="#0000aa"
    />
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
     android:gravity="center_vertical"
    android:text="yellow"
     android:layout_weight="1"
    android:background="#aaaa00"
    />
</LinearLayout>
 

 

 这里4个TextView控件都带有 android:layout_weight属性,且值都是1,所以将子LinearLayout的空间平均分成了4份。

android:gravity属性的作用是设置组件与 组建or文字 之间的对齐方式,主要有

center_vertical、center_horizontal、center、top、等等值,上面的代码效果如图:

 

如果将TextView改为 android:layout_width="wrap_content" ,效果变为:

 

 

 

各颜色条的宽度是不一致的,因为red,green,blue和yellow这几个字符串的长度不一样。

 

如果想既保持字符串不一致,又想得到相同宽度的颜色条,可以把TextView中的属性android:layout_width值改为"fill_parent"

 

将代码中红色TextView的layout_weight修改为2,显示效果如下:

由于字符串长度不一致,所以这里并不是2:1:1:1的比例.

 

原文来自:

http://hi.baidu.com/hbzha/blog/item/8af2b44f9bd8bd1eb2de055b.html

分享到:
评论

相关推荐

    2011.10.13(4)——— android android:layout_weight

    NULL 博文链接:https://trylovecatch.iteye.com/blog/1195121

    android_QQ_例子

    用Eclipse加载项目工程 &lt;LinearLayout xmlns:android=... android:layout_weight="0.66" android:background="@drawable/blue_bg" android:orientation="vertical" &gt; android:layout

    Android实训购物车页面

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

    Android购物车代码

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

    Android控件大全以及各布局空间的使用方式

    android:layout_weight="1" android:layout_height="wrap_content" android:text="行1列2" /&gt; &lt;TextView android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content...

    Android 百分比布局

    android:layout_weight="1" android:orientation="horizontal"&gt; android:id="@+id/tv_solid_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="本次上架...

    Android 重写RadioButton,模仿QQ消息气泡

    android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/icon_click_wd" android:gravity="center" android:text="我的" android:textSize="10dp" /&gt; --设置值,显示气泡 rb_...

    android水平导航条的实现

    我用的是HorizontalScrollView来实现水平条的滚动,按钮的背景宽度和图片的一致,以免被拉伸,main.xml的配置如下: &lt;LinearLayout xmlns:android=... android:layout_weight="1" &gt;

    Android实现图片自动轮换

    package xatu.cn.androidtest_one; import android.content.ContentResolver; import android.content.Intent;... android:layout_weight="1" /&gt; &lt;/android.support.constraint.ConstraintLayout&gt;

    九宫格牌翻转游戏demo

    android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop"/&gt; android:id="@+id/m3" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height=...

    WeChatSample

    android:layout_weight="1" android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="0dp"&gt;&lt;/android.support.v4.view.ViewPager&gt; android:paddingTop="10dp" android:background...

    Android中的android:layout_weight使用详解

    layout_weight的作用是设置子空间在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,则控件越重要,下面为大家介绍下具体的使用方法

    高仿微信界面

    android:layout_weight="1"&gt; &lt;/android.support.v4.view.ViewPager&gt; android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#737373" /&gt; android:layout_width=...

    WebViewDownloadTest.zip

    android:layout_weight="10" android:text="http://www.imust.cn/info/1062/5273.htm" /&gt; android:id="@+id/bt_go" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_...

    目录选择器源代码

    android:id="@+id/dir_str" android:gravity="center" android:layout_weight="1" /&gt; --&gt; android:id="@+id/btn_back" android:layout_width="40dip" android:layout_height="40dip" android:text="BACK" ...

    LCRapidDevelop-master

    android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="10dp...

    wkp111_StickLayout-粘性控件,其任意一个子控件都可滑动停留,本质为NestedScrollView和LinearLayout的结合。.zip

     android:layout_weight="1"  android:layout_width="0dp"  android:layout_height="match_parent"/&gt;    android:onClick="scrollTo3"  android:background="@android:color/holo_green_light"  android:...

    Parallax Everywhere-使ImageView和TextView产生视差效果.zip

     android:layout_weight="1"&gt;   android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_gravity="center"  android:layout_margin="10dp"  android:...

    android布局精解

    o 2.1、Tips:android:layout_weight="1" • 3、相对布局(Relative Layout) • 4、表格布局(Table Layout) • 5、列表视图(List View) o 5.1、一个小的改进 o 5.2、补充说明 • 6、网格视图(Grid View) ...

    SwipeListViewTest项目

    &lt;Button android:id="@+id/example_row_b_action_2" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_weight="1" ...

Global site tag (gtag.js) - Google Analytics