zoukankan      html  css  js  c++  java
  • [置顶] LinearLayout的layout_weight问题导致手机无法显示全部控件

    今天遇到个难缠的问题,Android界面一直都不是那么好设计,今天体会到了

    对应的layout文件如下:

    <?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="vertical" >
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="0dip"
            android:layout_weight="1">
            <TextView 
                android:text="@string/cost_time"
                android:layout_gravity="left"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"/>
             
            <Chronometer android:id="@+id/costTime"
                android:gravity="left"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
             
            <TextView 
                android:text="@string/best_time"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
            <TextView android:id="@+id/best_time"
                android:text="@string/default_time"
                android:layout_gravity="right"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"/>
        </LinearLayout>
        <LinearLayout android:id="@+id/puzzleview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="8">
             
        </LinearLayout>
        <LinearLayout android:id="@+id/utilbtn_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">
            <Button android:id="@+id/undo_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/undo_button"
                android:layout_weight="1"/>
            <Button android:id="@+id/erase_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/erase_button"
                android:layout_weight="1"/>"
        </LinearLayout>
        <LinearLayout android:id="@+id/num_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">
            <Button android:id="@+id/Button1"
                android:text="1"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
             
            <Button android:id="@+id/Button2"
                android:text="2"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
             
            <Button android:id="@+id/Button3"
                android:text="3"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
             
            <Button android:id="@+id/Button4"
                android:text="4"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
            <Button android:id="@+id/Button5"
                android:text="5"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
             
            <Button android:id="@+id/Button6"
                android:text="6"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
             
            <Button android:id="@+id/Button7"
                android:text="7"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
             
            <Button android:id="@+id/Button8"
                android:text="8"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
             
            <Button android:id="@+id/Button9"
                android:text="9"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dip"
                android:layout_weight="1"/>
        </LinearLayout>
     
    </LinearLayout>


    运行效果却很悲剧


    上面一小部分始终显示不出来,中间那个View为什么那么不肯妥协,非要占据上面的位置。

    这个问题困扰了我很长时间,问了很多人,有的说用RelativeLayout,但是我还是没有找到很好的解决方案


    后来上网查资料发现LinearLayout的layout_weight属性是成反比的,跟空间的大小分配刚好是相反的

    当时没太注意,后来百般无奈后,偶然间修改了那个View的layout的layout_weight

    <LinearLayout android:id="@+id/puzzleview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="18">
             
        </LinearLayout>


    运行下一看,好像有点变化,通过LogCat观察出

    每当我增大layout_weight时,onSizeChanged: width 35.5557:,height: 从45.33332逐渐减小到36.5557

    慢慢的,上面犹抱琵琶半遮面的Chronometer空间终于露出来了

    效果如图


    通过这几天的Android编程,感觉编程有的时候真的很苦逼,你常常为了一个奇怪的问题,茶不思饭不想,水不喝了,厕所不上了,妹子不找了,就一心想着这个问题弄不出来就一直弄下去

    但是当你解决问题时,那种感觉又是很美妙的,OK,编程还是要继续下去!!!

  • 相关阅读:
    JAVA 设计模式 组合模式
    JAVA 设计模式 模板方法模式
    SpringBoot 数据篇之使用JDBC
    [Spring]01_环境配置
    [spring]03_装配Bean
    [Spring]04_最小化Spring XML配置
    [Quartz笔记]玩转定时调度
    [Spring]支持注解的Spring调度器
    asp.net core 系列 13 日志
    asp.net core 系列 12 选项 TOptions
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3061709.html
Copyright © 2011-2022 走看看