问题描述
也许我误解了layout_weight参数,但我无法得到它,为什么这样的事情是不工作...
Maybe I misunderstood the layout_weight parameter but I can't get it why something like this is not working...
<?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" android:weightSum="1">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="TextView"
android:background="@color/apr_brightyellow"/>
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="TextView"
android:background="@color/apr_yellow"/>
</LinearLayout>
我有一个的LinearLayout内的三个或更多textviews。
我想将它们放置在列具有百分比宽度。
而这正是它产生:
I have three or more textviews within a LinearLayout.I want to place them in columns with a percentage width.And that is what it produces:
使用layout_weight设置好的0.3 / 0.2 / 0.5
With layout_weight setted to 0.3 / 0.2 / 0.5
然后,我改变了layout_weight参数只是有点如下:
Then I varied the layout_weight parameters just a bit as follows:
<?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" android:weightSum="1">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="TextView"
android:background="@color/apr_brightyellow"/>
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="TextView"
android:background="@color/apr_yellow"/>
</LinearLayout>
而现在我明白了,因为我想:
And now I get it as I wanted:
使用layout_weight设置好的0.3 / 0.2 / 0.5
With layout_weight setted to 0.3 / 0.2 / 0.5
是一个bug或者我真的误会关于layout_weight整个事情?
is it a bug or maybe I really misunderstood the whole thing about layout_weight?
推荐答案
当您使用的android:layout_weight
你应该设置的android:layout_width = 0dp
(在情况下,当垂直方向 - 的android:layout_height =0dp
)。
When you use android:layout_weight
you should set android:layout_width="0dp"
(in case when vertical orientation - android:layout_height="0dp"
).
这篇关于Android的Layout_weight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!