zoukankan      html  css  js  c++  java
  • 50一个Android开发技巧(01 利用好layout_weight属性)

    问题:如何将一个Button放置在布局的中间,并设置其宽度parent的50%?
    分析:问题想要达到的效果应该是这样:
    (原文地址:http://blog.csdn.net/vector_yi/article/details/24397733)

    这看起来不难,但非常多开发人员并不知道达到这样效果的最佳方法。


    解决:在此我们将weightSum属性与layout_weight属性一起利用。


    <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
        android:layout_width= "fill_parent"
        android:layout_height= "fill_parent"
        android:background= "#ffffff"
        android:gravity= "center"
        android:orientation= "horizontal"
        android:weightSum= "1" ><!--1.加入android:weightSum属性-->
    
        <Button
            android:layout_width ="0dp"<!--2.将Button的layout_width设为0dp-->
            android:layout_height ="wrap_content"
            android:layout_weight ="0.5"<!--3.确保其占用了50%的可用空间-->
            android:text ="@string/activity_main_click_me" />
    
    </LinearLayout>
    
    能够注意到,在第2步将Button的layout_width设为了0dp。会不会与layout_weight有冲突?答案是不会:
      一个控件的宽度是这样计算出来的:
      Widget's width + Widget's weight*Parent's width/Parent's weightSum


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    洛谷 P2053 :[SCOI2007]修车(拆点+最小费用流)
    LightOJ
    spark简单入门
    crontab 应用
    HttpClient的使用
    build.sbt的定义格式
    Scalatra
    SBT 构建scala eclipse开发
    mysql 存在更新,不存在插入
    Flash Vector例子
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4659237.html
Copyright © 2011-2022 走看看