Android布局权重

我是Android新手,我有一个关于在线性布局中设置权重的问题。

我正在尝试创build一个有两个自定义button和一个自定义编辑文本的行。 编辑文本应该只占用与其内容相同的空间,并且两个button应该水平扩展以填充行中剩余的可用空间。 喜欢这个…

| [#Button++#] [#Button--#] [et] | 

经过多次尝试,这是最接近我可以得到我想要的,即使它似乎过于复杂。

 | [Button] [Button] [###ET###] | 

 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal|center_vertical" android:layout_weight="1" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" > <RelativeLayout android:id="@+id/btn_plus_container" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageButton android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btn_plus" android:background="@drawable/btn" android:layout_centerInParent="true" ></ImageButton> <TextView android:textColor="#FAFAF4" android:id="@+id/tv_dice_plus" android:text="@string/tv_plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" ></TextView> </RelativeLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" > <RelativeLayout android:id="@+id/btn_minus_container" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageButton android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btn_minus" android:background="@drawable/btn" android:layout_centerInParent="true" ></ImageButton> <TextView android:textColor="#FAFAF4" android:id="@+id/btn_minus" android:text="@string/tv_minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" ></TextView> </RelativeLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" > <EditText android:textColor="#FAFAF4" android:text="@string/et_output" android:id="@+id/et_output" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:selectAllOnFocus="true" android:minWidth="50sp" android:maxWidth="50sp" android:background="@drawable/tv_black_background3" android:textColorHighlight="#c30505" ></EditText> </LinearLayout> </LinearLayout> 

据我了解设置[android:layout_weight =“2”]为线性布局按住button应使他们占用更多的空间比编辑文本,但它做了相反的我,使他们的一半大小。

 | [Btn] [Btn] [#####et#####] | 

我尝试了很多不同的组合,我甚至不记得它们,也没有任何组合。

它不工作,因为你使用fill_parent作为宽度。 当总和大于LinearLayout时,权重用于分配剩余空间占用空间。 将你的宽度设置为0dip ,而且会起作用。

android:Layout_weight可以用在你没有像你的fill_parent一样给你的宽度添加修正值

做这样的事情:

 <Button> Android:layout_width="0dp" Android:layout_weight=1 -----other parameters </Button> 

这样想,会更简单

如果你有3个button,它们的权重是1,3,1,那么它就像HTML中的表一样工作

为该行提供5个部分:1个button1部分,3个button2部分和1个button1部分

在linearLayout中设置WeightSum = 2;

并将重量分配给它的孩子,因为你想要他们显示..我已经给孩子重量=“1”,所以两者将分配总数的一半。

  <LinearLayout android:id="@+id/linear1" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" android:orientation="horizontal" > <ImageView android:id="@+id/ring_oss" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/ring_oss" /> <ImageView android:id="@+id/maila_oss" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/maila_oss" /> </LinearLayout> 

权重值0-1与权重值成比例地分配可用空间的分配(在设置layout_width =“0px”之后)。 查看没有指定重量的元素(没有重量input)得到重量0,这意味着他们没有扩展。

一个简单的替代scheme,不需要重量条目是将选取框附加到文本视图,该文本告诉它从文本(wrap_content)的最小需求扩展到可用空间EditText:android:ellipsize =“marquee”

我会假设将EditText的宽度设置为wrap_content ,并将两个button放入宽度为fill_parent和权重设置为1的LinearLayout

我发现的另一个原因(听起来很模糊)。 下面没有工作。

LinearLayout垂直

LinearLayout高度fillparent +重量

LinearLayout高度fillparent +重量

LinearLayout高度fillparent +重量

EndLinearLayout

什么工作是

的RelativeLayout

LinearLayout垂直

LinearLayout高度fillparent +重量

LinearLayout高度fillparent +重量

LinearLayout高度fillparent +重量

EndLinearLayout

EndRelativeLayout

这听起来很模糊,根据线性的根布局和权重下不起作用。 而当我说“没有工作”的时候,我的意思是,在我看了各种分辨率之间的graphics布局之后,屏幕一致性打破了大的时间。

 <LinearLayout android:id="@+id/linear1" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="9" android:orientation="horizontal" > <ImageView android:id="@+id/ring_oss" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:src="@drawable/ring_oss" /> <ImageView android:id="@+id/maila_oss" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:src="@drawable/maila_oss" /> <EditText android:id="@+id/edittxt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:src="@drawable/maila_oss" /> </LinearLayout>