android中weight的使用
2016-04-09 10:38
453 查看
在xml布局中一不小心写了这样一个布局..
我原来想实现的是button_1所占比例为1,button_2所占比例为2.
可结果为:
![](https://img-blog.csdn.net/20160409102707950)
原来button所占的宽度是这样计算的:计算出来的宽度=按钮设置的宽度+剩余空间所占百分比宽度。
则第一个button的宽度为:L+(L-2L)*1/3=2/3L。
第二个button的宽度为:L+(L-2L)*2/3=1/3L。
要达到我们所需要的效果,即这样修改:
就能达到button_1所占比例为1,button_2所占比例为2;
修改后第一个button的宽度:0+L*1/3=1/3L;
小知识,怕以后会忘掉~~
<?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" android:orientation="horizontal" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/btn1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="@string/btn2"/> </LinearLayout>
我原来想实现的是button_1所占比例为1,button_2所占比例为2.
可结果为:
原来button所占的宽度是这样计算的:计算出来的宽度=按钮设置的宽度+剩余空间所占百分比宽度。
则第一个button的宽度为:L+(L-2L)*1/3=2/3L。
第二个button的宽度为:L+(L-2L)*2/3=1/3L。
要达到我们所需要的效果,即这样修改:
<?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" android:orientation="horizontal" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/btn1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="@string/btn2"/> </LinearLayout>
就能达到button_1所占比例为1,button_2所占比例为2;
修改后第一个button的宽度:0+L*1/3=1/3L;
小知识,怕以后会忘掉~~
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories