Android实习收获:UI细节bug引发的layout

今天在修改一个布局问题时候,发现自己对权重的理解还不够。首先问题如图:

一个TextView没有按要求显示完整,显示成了2行。怎么办呢?

方法1:是把它左面的字体放小。 结果师兄说不可以随意修改布局的尺寸,否则测试还会提bug。

方法2:不让改字体,那就修改边距,,图片的margin,textView的magin,统统改了遍。 结果可想而知,这么大的变动更不可以。

师兄看我这么愚钝,指点了一下:不修改布局,怎样能让时间不论在什么情况下都显示呢?

我的做法:<TextViewandroid:id=”@+id/teacher_name”android:layout_width=”wrap_content”android:layout_height=”wrap_content”android:text=”张文欣老师回答了您的问题”android:textColor=”@color/color_blackest_text”android:textSize=”@dimen/text_size_mediumer” /><TextViewandroid:id=”@+id/notice_time”android:layout_width=”0dp”android:layout_height=”wrap_content”android:layout_weight=”1″android:singleLine=”true”android:layout_marginLeft=”3dp”android:text=”5-16 4:04″android:textSize=”@dimen/text_size_smaller” />显示布局:

还是那样。为什么呢? 设置layout_weight = “1”不就是为了保证它在什么情况下都显示吗?怎么回事。

4。师兄看不下去了,亲自示范给我,代码如下:

<TextViewandroid:id=”@+id/teacher_name”android:layout_width=”0dp”android:layout_height=”wrap_content”android:layout_weight=”1″android:singleLine=”true”android:text=”张文欣老师回答了您的问题”android:textColor=”@color/color_blackest_text”android:textSize=”@dimen/text_size_mediumer” /><TextViewandroid:id=”@+id/notice_time”android:layout_width=”wrap_content”android:layout_height=”wrap_content”android:layout_marginLeft=”3dp”android:text=”5-16 4:04″android:textSize=”@dimen/text_size_smaller” />哇塞,这次ok了:

虽然一样都是设置layout_weight,但是设置在什么位置就显示出了对它的理解。我之前的理解一直都是把某个组件的宽或者高设置为0,然后再设置个权重为1,它就会填充所有剩下的空间。事实上很多时候也奏效了。今天这次让我仔细看了之前的一篇文章【Android入门的常见问题Part1】android:layout_weight的理解,结合这次的问题,好好思考了一下layout_weight的用法。用法总结:

最常见的使用权重都是设置宽或者高为0dp,然后设置权重为1.而且整个布局中只有这一个权重。

还有的时候我们布局中不仅一个权重,比如说为了屏幕适配,给布局中的所有子组件都设置权重,那么子组件就会占据权重响应的比例。

比如说:=======>

– button1会占据2/3的位置,button2会占据1/3. – 注意,两个Button的宽度都是0dp

3.在2的情况下,我们设置Button的宽度为wrap_content:

=======>

4.在3的情况下,我们设置Button的宽度为match_parent:

=======>

这就解释了为什么有时候weight值大,占有的权重反而小。权重所占的比例与对应的宽度、高度有关,视情况而定,不可死记硬背。不过要理解的就是分配的是剩下的空间,优先级最低。

不是每一次努力都有收获,但是,每一次收获都必须经过努力。

Android实习收获:UI细节bug引发的layout

相关文章:

你感兴趣的文章:

标签云: