`
一口三个汉堡
  • 浏览: 111404 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

android线性布局布局的layout_weight

阅读更多

对于LinearLayout的layout_weight属性,很多人都很了解,今天我把我的一本android书又从第一页开始翻,特意自己写了demo来测试这个属性,发现了一个很有趣的现象.
android_width会影响到android_weight

看下面了两个布局文件,他们的差别只是layout_width的值.一个fill_parent,另一个是wrap_content

<?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="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="111"
    android:background="#ff0000"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2"
    android:text="22"
    android:background="#00ff00"
    />
   <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="3"
    android:background="#0000ff"
    android:text="33"
    />
</LinearLayout>
 

 

<?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="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="111"
    android:background="#ff0000"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2"
    android:text="22"
    android:background="#00ff00"
    />
   <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="3"
    android:background="#0000ff"
    android:text="33"
    />
</LinearLayout>
 

 

 上面的布局代码分别对应下面的两张图片,出来的效果是完全不一样的,所以当在使用layout_weight的时候,layout_width的值也是需要关心的.
  • 大小: 4.9 KB
  • 大小: 5.2 KB
0
1
分享到:
评论

相关推荐

    4种Android屏幕自适应解决方案

    该属性的作用是决定控件在其父布局中的显示权重,一般用于线性布局中。其值越小,则对应的layout_width或layout_height的优先级就越高,一般横向布局中,决定的是layout_width的优先级;纵向布局中,决定的是layout_...

    【Android Studio代码】线性布局

    用Android Studio 2.3.3做的线性布局例子,主要用到的标签有:&lt;LinearLayout&gt;&lt;/LinearLayout&gt;;主要用到的设置有:android:orientation="horizontal"、android:layout_weight="1"。

    android布局精解

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

    Android基础 布局、数据存储访问、XML系列化解析和SharedPreferences入门

    对于屏幕适配,可以利用线性布局中的layout_gravity和gravity以及配合weight调整界面布局;同时可以利用相对布局中的4组属性调整布局。 2、Android测试 利用monkey进行压力测试,利用Junit进行单元测试 3、数据...

    Android布局.rar

    View 的布局显示方式有下面几种:线性布局(Linear Layout)、相对布局(Relative Layout)、表格布局(Table Layout)、网格视图(Grid View)、标签布局(Tab Layout)、列表视图(List View)、绝对布局...

    Android布局之LinearLayout线性布局

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 常用属性: android:gravity————设置的是控件自身上面的内容位置 android:layout_gravity—–设置控件本身相对于父控件的显示位置 android:layout_...

    android Activity线性布局和表格布局实例讲解

    其中本实验用到的各控件的属性解释如下:android:layout_weight属性是指不同的控件在activity中占有体积大小的比例。android:paddingLeft指内边距左的距离,即控件内文字离控件左边边界的距离。其它的类推。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...

    linearLayout线性布局

    基础属性 高度height,宽度width,排列方式...android:layout_weight u201c1” android:layout_height=“match_parent” android:background&gt; 运行效果如下 因为灰色部分和黑色部分权重相同故两部分的宽度各占总宽

    androidlayout各种布局

    LinearLayout(线性布局)提供了控件水平垂直排列的模型,同时可以通过设置子控件的weight布局参数控制各个控件在布局中的相对大小。水平(vertical)垂直(horizontal)fill-parent:占满整个屏幕,wrap-content:刚好适合...

    Android制作漂亮自适布局键盘的方法

    最常见的解决方案是用线性布局,自适应当然是按比例,但布局中无%的概念,那就要用到layout_weight了,该属性的作用是决定控件在其父布局中的显示权重(具体概念就不多说了)。  这里用一个LinearLayout 将数字...

    井字游戏

    这是一个Android学生项目,使用常见的UI元素,例如-Button -TextView... 面板的UI元素由九个按三行线性布局排列的按钮组成。按钮的宽度为零,其整齐的布局通过将其layout_weight =“ 1”(LinearLayout)放置来进行处理

    exemplo-jogo-da-velha:使用二维数组的 Android 井字游戏示例

    所有按钮的权重为 1(android:layout_weight="1")。 这样,按钮均匀地占据整个屏幕。 这些方法检查每轮是否有赢家。 如果找到获胜者,该方法会更改结果行的颜色并显示获胜者的消息(Toast 消息)。 我们使用二维...

Global site tag (gtag.js) - Google Analytics