//网站根目录 var __ID_HOME__="#zj_home"; //生产环境设置为true,本地设置为false var __JS_DEBUG__= true; var __JS_APP__ = ""; var __JS_APP_HOME__ = "/"; var __JS_PUBLIC__ = "/Public"; var __JS_HOME__ = "/Template/default/Home/Public"; var _URL_SIGN = "https://zhangjunbk.com:443/sign"; var _URL_LOGIN = "https://zhangjunbk.com:443/login"; var _URL_REG = "https://zhangjunbk.com:443/reg"; var _URL_FORGET = "https://zhangjunbk.com:443/forget"; var _URL_SEND_EMAIL = "https://zhangjunbk.com:443/sendEmail"; var _URL_SEND_MOBILE = "https://zhangjunbk.com:443/sendMobile"; var _URL_RELATION_EMAIL = "https://zhangjunbk.com:443/relationEmail"; var _URL_SIGN_RELATION_EMAIL = "https://zhangjunbk.com:443/sign_relation"; var _URL_FRONT_LOGOUT="/frontlogout"; var _URL_CHECK_LOGIN = "https://zhangjunbk.com:443/check_login"; var _URL_PAY_POINTS = "https://zhangjunbk.com:443/payPoints"; var _URL_PAY_POINTS_VIDEO = "https://zhangjunbk.com:443/payPointsVideo"; var _URL_USER_POINTS = "https://zhangjunbk.com:443/uindex"; var _URL_AJAX_STOCK_SEALING = "https://zhangjunbk.com:443/ajaxStockSealing"; var _URL_API_WX_CREATE = "https://zhangjunbk.com:443/wxV2Create"; var _URL_API_WX_CREATE_NATIVE_REWARD = "https://zhangjunbk.com:443/wxV2CreateNative_reward"; var _URL_CENTER_USER = "https://zhangjunbk.com:443/centerUser"; var _URL_QQ = "{https://www.zhangjunbk.com/oauth/callback/}";

安卓按比例布局,layout_weight用法

系统 1970 0

在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情况,然后自己通过实验和一些比较好的文章总结了一下,特此记录下来,以备以后所用。Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性。
1.当控件的属性android:layout_width="fill_parent"时,布局文件如下:
Xml代码
<?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">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="Button1" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="2"
android:text="Button2" />
</LinearLayout>
在这里Button1的Layout_weight=1,Buttong2的Layout_weight=2,运行效果为:

安卓按比例布局,layout_weight用法
我们看到,Button1占了2/3,Button2占了1/3。如果此时把button2的weight设置成2000,不是说Button2就消失了,而是Button1的宽度几乎占满了屏幕宽度,而屏幕最后一丝细条则是留给Button2的,已近非常小了,没有显示出来。截图如下:


得出结论:在layout_width设置为fill_parent的时候,layout_weight代表的是你的控件要优先尽可能的大,但尽可能大是有限度的,即fill_parent.

2.当控件的属性android:layout_width="wrap_content"时,布局文件如下:

Xml代码
<?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">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="Button1" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="2"
android:text="Button2" />
</LinearLayout>
同样,Button1的weight设置为1,Button2的weight设置为2,但是效果与fill_parent的效果截然相反。运行效果如下:

安卓按比例布局,layout_weight用法
这时,和fill_parent正好相反,Button1的宽度占据了屏幕宽度的1/3,而Button2的宽度占据了屏幕的2/3,如果此时把Button1的weight设置为2000,按照之前理解,Button1应该小的几乎在屏幕上看不到,但是错了,实验告诉我们,当Button1的weight非常小时,也要"wrap_content",也就是要保证Button1至少能够显示。以下是Button1设置weight为2000时的运行截图:

安卓按比例布局,layout_weight用法
我们看到,Button1已经足够小,但是要保证他能显示出来,因此得出结论:
在layout_width设置为wrap_content的时候,layout_weight代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
当了解这些后,我们再设计程序时,为了能够自适应屏幕,不想给控件一个指定的宽度和高度,就可以使用这个weight属性来让它按自己比例来划分屏幕高度或者宽度了。


说了他们的主要意义,其实还有更简单的用法,可以直接把控件宽高设为零,然后设置layout_weight,即可按屏幕比例划分,例如一行两个控件,一个设为2,一个设为3,即一个占宽高2/5和3/5。

安卓按比例布局,layout_weight用法


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论