`

Android LinearLayout的android:layout_weight属性

 
阅读更多

本文主要介绍Android LinearLayout的android:layout_weight属性意义

 

android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根据LinearLayout中所有相关元素的此属性值计算的。

除了已经固定大小的,其他设置了此属性的view所占大小(长度或高度)为自己layout_weight属性值/所有layout_weight属性值*总大小。这个属性在android的sdk中都没有介绍。下面举例介绍下

 

比如在一个layout中显示3个TextView,第一个TextView长度占20%,第二个长度占50%,第三个占长度30%,

则比例为20%:50%:30%=2:5:3。layout代码如下

<?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">   
   	<TextView android:id="@+id/textView1"   
   		android:layout_width="wrap_content"   
   		android:layout_height="match_parent"   
   		android:layout_weight="2"
   		android:layout_alignParentLeft="true"
   		android:gravity="center_vertical"
  		android:text="文本1" /> 
   	<TextView android:id="@+id/textView2"   
   		android:layout_width="wrap_content"   
   		android:layout_height="match_parent"   
   		android:layout_weight="5"
   		android:layout_alignParentLeft="true"
   		android:gravity="center_vertical"
  		android:text="文本2" /> 
   	<TextView android:id="@+id/textView3"   
   		android:layout_width="wrap_content"   
   		android:layout_height="match_parent"   
   		android:layout_weight="3"
   		android:layout_alignParentLeft="true"
   		android:gravity="center_vertical"
  		android:text="文本3" /> 
</LinearLayout>

从以上代码可以看出只需要设置各个TextView的android:layout_weight属性值为对应的比例即可

其中android:layout_alignParentLeft="true" android:gravity="center_vertical"是为了方便查看而设置

 

 

 

分享到:
评论

相关推荐

    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_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控件大全以及各布局空间的使用方式

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

    android水平导航条的实现

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

    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; &lt;LinearLayout android:paddingTop="10dp" ...

    九宫格牌翻转游戏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=...

    Android App中的多个LinearLayout嵌套布局实例解析

    如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的, 换句话说最外层不能用layout_weight xml布局如下 &lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:...

    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中的android:layout_weight使用详解

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

    Android实现图片自动轮换

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

    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" ...

    CircleIndicator-一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果.zip

     android:layout_weight="1"&gt;  &lt;android.support.v4.view.ViewPager  android:id="@ id/viewpager_default"  android:layout_width="match_parent"  android:layout_height="match_parent"/&gt;    ...

    高仿微信界面

    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=...

    Android layout_weight使用方法及实例

    &lt;LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”horizontal”&gt; &lt;...

    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:...

    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...

    android layout 按比例布局的代码

    为了创建比例大小的子View,可以将LinearLayout的宽度和...对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextVie

    目录选择器源代码

    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" ...

    Android应用中通过Layout_weight属性用ListView实现表格

    今天主要说的是对Layout_weight属性的完全解析,以及利用Layout_weight这个属性使用ListView来实现表格的效果,我们都知道Android里面专门有一个TableLayout来实现表格的,说实话,我平常开发中用TableLayout还是...

Global site tag (gtag.js) - Google Analytics