对于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>
![](http://dl.iteye.com/upload/attachment/565411/e78e1f92-7c23-36e1-b03c-496e6c77f385.png)
上面的布局代码分别对应下面的两张图片,出来的效果是完全不一样的,所以当在使用layout_weight的时候,layout_width的值也是需要关心的.
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0056/5409/40098940-2d85-3c5f-9c46-ae588fc92baa-thumb.png)
- 大小: 4.9 KB
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0056/5411/e78e1f92-7c23-36e1-b03c-496e6c77f385-thumb.png)
- 大小: 5.2 KB
分享到:
相关推荐
在Android开发中,线性布局(LinearLayout)是基础且常用的布局管理器之一,它允许开发者按照垂直或水平方向排列子视图(View)。本实例针对初学者,将深入讲解线性布局的使用方法和特点。 一、线性布局介绍 线性...
- 在线性布局中,通过设置`android:layout_weight`属性,可以让子视图按比例分配空间。当一个视图的`layout_width`或`layout_height`设置为"0dp"时,`layout_weight`才会生效。例如,两个子视图的`layout_weight`...
线性布局虽然简单,但通过合理运用`layout_weight`和其他属性,可以构建出多样化的UI布局。 例如,如果我们想要创建一个水平排列的按钮栏,其中每个按钮的宽度相等,我们可以这样做: ```xml android:...
2. 使用`android:weightSum`属性可以为线性布局内的子视图设置一个总的权重限制。 3. 在实际开发中,为了适配不同屏幕尺寸,应尽量使用`dp`单位而不是像素单位,以保持视觉一致性。 通过下载并研究" Myfirst1 "这个...
在Android开发中,线性布局(LinearLayout)是基础且至关重要的布局管理器,它用于组织UI元素(如按钮、文本视图等)沿单一方向排列,可以是垂直或水平。本篇文章将深入探讨线性布局的使用方法、属性以及如何在实际...
除了`layout_weight`外,还可以通过`android:orientation`属性来指定线性布局的方向,值可为`vertical`(垂直排列)或`horizontal`(水平排列)。例如: ```xml android:orientation="vertical"> <!-- 子视图将...
在`LinearLayoutDemo`这个实例中,开发者可能会演示如何创建一个包含多个按钮或文本视图的线性布局,展示如何通过设置`layout_weight`来实现动态的界面布局。例如,可能有一个包含三个按钮的水平线性布局,每个按钮...
本篇文章将深入探讨两种最常用的布局管理器——线性布局(LinearLayout)和相对布局(RelativeLayout),它们是Android开发者必须掌握的核心知识。 线性布局是Android中最基础的布局方式,如同它的名字所示,它会...
首先,线性布局是Android中最基础的布局方式,它可以将子视图按照垂直或水平的方向进行排列。线性布局的主要优点在于其简洁和易用,适用于简单的UI设计。线性布局有两个关键属性:`orientation`和`weight`。`...
该属性的作用是决定控件在其父布局中的显示权重,一般用于线性布局中。其值越小,则对应的layout_width或layout_height的优先级就越高,一般横向布局中,决定的是layout_width的优先级;纵向布局中,决定的是layout_...
线性布局(LinearLayout)和相对布局(RelativeLayout)是Android中两种最常用的布局管理器,用于控制应用程序界面中各个组件的排列和对齐方式。本篇文章将深入探讨这两种布局,并通过实例展示它们在实际应用中的...
本文将深入探讨五种主要的布局控件:线性布局(LinearLayout)、框架布局(FrameLayout)、表格布局(TableLayout)、相对布局(RelativeLayout)以及约束布局(ConstraintLayout),并提供基础使用方法。...
线性布局中的`layout_weight`属性是其核心功能之一,它可以用来分配子组件的相对空间。当设置`layout_weight`时,布局会优先考虑权重分配的空间,而不是每个组件自身的尺寸。例如,如果两个TextView的`layout_weight...
在线性布局中,可以通过设置`android:layout_weight`属性来调整子视图的相对大小。权重越高,分配的空间越大。权重值可以是浮点数,如: ```xml android:layout_width="0dp" <!-- 必须设为0dp,权重才能生效 --> ...
Android线性布局之计算器的...Android线性布局之计算器的界面实现是一种复杂的界面布局,需要熟悉线性布局的基本概念、控件的属性、布局weight属性等知识点。只有掌握了这些知识点,才能更好地实现计算器的界面布局。
本篇文章将深入探讨Android的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、AbsoluteLayout(绝对布局)以及TableLayout(表格布局),并提供源码范例来帮助理解。...
1. **LinearLayout**:线性布局是最基础的布局,它按照垂直或水平方向将子视图(View)按顺序排列。通过设置`android:orientation`属性可以选择排列方向,`android:weightSum`可以分配权重,使子视图按照比例分配...
线性布局(Linear Layout)是Android开发中常用的一种布局方式,它按照垂直或水平方向将子视图(View)逐一排列。在Android应用界面设计中,线性布局扮演着基础构造角色,允许开发者以简单直观的方式组织用户界面...
在Android开发中,线性布局(LinearLayout)是常见的布局管理器之一,它允许开发者按照垂直或水平方向排列子视图。在本示例中,我们将讨论如何使用线性布局来实现一个简单的计算器界面。这个计算器界面包含一个...
线性布局提供了对齐方式的设置,包括`android:gravity`和`android:layout_gravity`两个属性。前者设置整个布局内部子视图的对齐方式,后者设置单个子视图在父布局中的对齐方式。对齐方式包括居中、左对齐、右对齐、...