Android开发之线性布局详解(布局权重)
布局权重
线性布局支持给个别的子视图设定权重,通过android:layout_weight属性。就一个视图在屏幕上占多大的空间而言,这个属性给其设 定了一个重要的值。一个大的权重值,允许它扩大到填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组剩余的其他的空间将会分配给其声明权 重的子视图。默认的权重是0;
未使用权重前效果图:
俩个线性布局组件,代码如下:
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
03.
android:layout_width
=
"match_parent"
04.
android:layout_height
=
"match_parent"
05.
android:orientation
=
"vertical"
>
06.
<
ScrollView
07.
android:id
=
"@+id/scrollView_content"
08.
android:layout_width
=
"match_parent"
09.
android:layout_height
=
"wrap_content"
10.
android:layout_gravity
=
"center_horizontal"
11.
12.
android:orientation
=
"vertical"
>
13.
<
LinearLayout
14.
android:layout_width
=
"wrap_content"
15.
android:layout_height
=
"wrap_content"
16.
android:layout_gravity
=
"center_horizontal"
17.
android:orientation
=
"vertical"
>
18.
<
ImageView
19.
android:layout_width
=
"wrap_content"
20.
android:layout_height
=
"wrap_content"
21.
android:layout_gravity
=
"center_horizontal"
22.
android:src
=
"@drawable/message_selected"
/>
23.
<
TextView
24.
android:layout_width
=
"wrap_content"
25.
android:layout_height
=
"wrap_content"
26.
android:layout_gravity
=
"center_horizontal"
27.
android:padding
=
"10dp"
28.
android:text="测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n
29.
"
30.
android:textSize
=
"20sp"
/>
31.
</
LinearLayout
>
32.
</
ScrollView
>
33.
<
LinearLayout
34.
android:layout_width
=
"wrap_content"
35.
android:layout_height
=
"wrap_content"
36.
android:layout_gravity
=
"center_horizontal"
37.
android:orientation
=
"horizontal"
>
38.
<
Button
39.
android:id
=
"@+id/btn1"
40.
android:layout_width
=
"wrap_content"
41.
android:layout_height
=
"wrap_content"
42.
android:text
=
"嵌套Fragment"
/>
43.
<
Button
44.
android:id
=
"@+id/btn2"
45.
android:layout_width
=
"wrap_content"
46.
android:layout_height
=
"wrap_content"
47.
android:text
=
"外部Fragment"
/>
48.
</
LinearLayout
>
49.
</
LinearLayout
>
仔细看下和想下就会发现这有个严重的问题,那就是“测试文字”行数多有,第二个LinearLayout布局被挤压或挤出显示区,如下图:
解决方法如下,在第一个LinearLayout 中加入权重android:layout_weight="1",代码如下:
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
03.
android:layout_width
=
"match_parent"
04.
android:layout_height
=
"match_parent"
05.
android:orientation
=
"vertical"
>
06.
<
ScrollView
07.
android:id
=
"@+id/scrollView_content"
08.
android:layout_width
=
"match_parent"
09.
android:layout_height
=
"wrap_content"
10.
android:layout_gravity
=
"center_horizontal"
11.
android:layout_weight
=
"1"
12.
android:orientation
=
"vertical"
>
13.
<
LinearLayout
14.
android:layout_width
=
"wrap_content"
15.
android:layout_height
=
"wrap_content"
16.
android:layout_gravity
=
"center_horizontal"
17.
android:orientation
=
"vertical"
>
18.
<
ImageView
19.
android:layout_width
=
"wrap_content"
20.
android:layout_height
=
"wrap_content"
21.
android:layout_gravity
=
"center_horizontal"
22.
android:src
=
"@drawable/message_selected"
/>
23.
<
TextView
24.
android:layout_width
=
"wrap_content"
25.
android:layout_height
=
"wrap_content"
26.
android:layout_gravity
=
"center_horizontal"
27.
android:padding
=
"10dp"
28.
android:text="测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n测试文字\n
29.
"
30.
android:textSize
=
"20sp"
/>
31.
</
LinearLayout
>
32.
</
ScrollView
>
33.
<
LinearLayout
34.
android:layout_width
=
"wrap_content"
35.
android:layout_height
=
"wrap_content"
36.
android:layout_gravity
=
"center_horizontal"
37.
android:orientation
=
"horizontal"
>
38.
<
Button
39.
android:id
=
"@+id/btn1"
40.
android:layout_width
=
"wrap_content"
41.
android:layout_height
=
"wrap_content"
42.
android:text
=
"嵌套Fragment"
/>
43.
<
Button
44.
android:id
=
"@+id/btn2"
45.
android:layout_width
=
"wrap_content"
46.
android:layout_height
=
"wrap_content"
47.
android:text
=
"外部Fragment"
/>
48.
</
LinearLayout
>
49.
</
LinearLayout
>
效果如下图
这样就算文字内容再长也不会把第二个LinearLayout 挤出显示区或挤压,权重布局原理图
Android开发之线性布局详解(布局权重)的更多相关文章
- Android开发 ExpandableListView 可折叠列表详解
前言 在需要实现一个List的item需要包含列表的时候,我们就可以选择ExpandableListView. 其实这个View的原始设计还是ListView的那套.就是增加2层的ListView而已 ...
- Android开发:程序目录结构详解
HelloWorld程序的目录结构概述 我们可以在文件夹中看到,HelloWorld程序的目录主要包括:src文件夹.gen文件夹.Android文件夹.assets.res文件夹. AndroidM ...
- Android开发——事件分发机制详解
0. 前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52566965 深入学习事件分发机制,是为了解决在Android开发中 ...
- Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)
在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...
- Android 开发 MaterialDialog框架的详解
前言 开始之前还是需要废话一下,因为有一些坑需要告知.首先MaterialDialog在GitHub上作者已经转型使用100% Kotlin语言编写,虽然可以在Java里调用Kotlin使用.但是个人 ...
- android开发之自定义View 详解 资料整理 小冰原创整理,原创作品。
2019独角兽企业重金招聘Python工程师标准>>> /** * 作者:David Zheng on 2015/11/7 15:38 * * 网站:http://www.93sec ...
- android 开发 View _5_ Paint详解
转载:http://blog.csdn.net/abcdef314159 //Paint的setStyle,Style共有3种 setStyle(Style style) Paint.Style.FI ...
- android开发之动画的详解 整理资料 Android开发程序小冰整理
/** * 作者:David Zheng on 2015/11/7 15:38 * * 网站:http://www.93sec.cc * * 微博:http://weibo.com/mcxiaob ...
- Android 开发 存储目录的详解
简介 Android设备,有3个地方的文件存储位置,他们分别是: 内部存储空间(用户无法浏览到此目录) 外部存储空间(就是手机自身的文件管理目录,用户可以浏览) SD卡的存储空间(需要插入T卡) Sh ...
- Android开发——HandlerThread以及IntentService详解
.HandlerThread Android API提供了HandlerThread来创建线程.官网的解释是: //Handy class for starting a new thread that ...
随机推荐
- MVC 与 MVP 架构 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 让两个DIV的高度隐式同步
以前遇到两个相临近的块,高度要一样,但是内容多少又不定时,我都是通过把这两块封装在TD里面实现,但今天在CSDN上面看到有人要通过JS来实现这个,我尝试了一下.http://topic.csdn.ne ...
- SharpDevelop 版本信息
mscorlib,4.0.0.0,C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll SharpDevelop,5.1.0.4936, ...
- 织梦DeDeCms会员登录或退出跳转到首页的修改方法
会员在主页登陆后,默认会跳转到会员中心,如果我们想登陆后,跳转到网站主页,那么就请参考下面的方法实现织梦DeDeCms会员登录或退出跳转到首页. 1.在根目录的member目录中找到index_do. ...
- Android运行机制
一. Android平台各层 Android平台架构图,如下图:由上到下依次为应用程序.应用程序框架.库.Android运行时.Linux内核. 1.Linux内核: Android系统基于Linux ...
- Java的类名与文件名必须一致(转)
原文:http://blog.csdn.net/shaoxiaoning/article/details/40424087 1.Java保存的文件名必须与类名一致: 2.如果文件中只有一个类,文件名必 ...
- 算法笔记_206:第五届蓝桥杯软件类决赛真题(Java语言A组)
目录 1 海盗分金币 2 六角幻方 3 格子放鸡蛋 4 排列序数 5 幂一矩阵 6 供水设施 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 海盗分金币 有5个海盗,相约进行一次帆船比赛. 比 ...
- html-webpack-plugin插件 根据模板生成多页面
1.项目目录结构为: 2.webpack.config.js配置文件为: var htmlWebpackPlugin = require('html-webpack-plugin'); module. ...
- C10K——千万级并发实现的秘密:内核不是解决方案,而是问题所在!(转)
既然我们已经解决了 C10K并发连接问题,应该如何提高水平支持千万级并发连接?你可能会说不可能.不,现在系统已经在用你可能不熟悉甚至激进的方式支持千万级别的并发连接. 要知道它是如何做到的,我们首先要 ...
- Unity 文字爆炸(风化)消失效果 粒子系统应用
利用Unity的粒子系统,使用C#代码控制粒子的位置和速度,实现文字风化爆炸的效果. Unity的东西,不像flash,不能直接放到网页中,没办法了,只能放截图了.有兴趣的可以下载看看:text_ex ...