Android 当有两个相同大小的按钮加上一个在中心拉伸时,LinearLayout文本换行问题
看起来有很多关于居中、相同尺寸等的问题,但到目前为止,我没有找到与我的情况完全一致的答案,所以我敢问:) 我需要的是三个按钮的布局,如下所示:Android 当有两个相同大小的按钮加上一个在中心拉伸时,LinearLayout文本换行问题,android,layout,android-linearlayout,Android,Layout,Android Linearlayout,看起来有很多关于居中、相同尺寸等的问题,但到目前为止,我没有找到与我的情况完全一致的答案,所以我敢问:) 我需要的是三个按钮的布局,如下所示: [ previous ][*select*] [ next ] 其中,[previous]和[next]按钮的大小相同(即在这种情况下,[previous]按钮的大小越大),并且[*选择*]按钮应拉伸以占据所有可用宽度 根据在LinearLayout中制作两个大小相同的按钮的提示,我生成了以下xml文件: <LinearLayout
[ previous ][*select*] [ next ]
其中,[previous]和[next]按钮的大小相同(即在这种情况下,[previous]按钮的大小越大),并且[*选择*]按钮应拉伸以占据所有可用宽度
根据在LinearLayout中制作两个大小相同的按钮的提示,我生成了以下xml文件:
<LinearLayout
android:id="@+id/button_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Previous" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Select" />
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Next" />
</LinearLayout>
不知道这是不是一个错误,但你能给我一个解决办法或其他一些方法来实现所需的布局
谢谢大家! 在这种情况下,我会选择TableLayout,其中有一行,每个按钮的权重为1。并将stretchColumn属性置于第1列。这有什么区别吗?除了在代码中这样做之外,我不确定您是否可以将一个大小与其他大小进行匹配。最简单的解决方案可能是调整dp/sp单元中的“上一个”和“下一个”按钮宽度,直到它们看起来很好,并且“选择”按钮是唯一具有“布局权重”属性的按钮。我建议使用相对长度
<RelativeLayout
android:id="@+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false">
<Button
android:id="@+id/previous"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="Previous"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/next"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentRight="true"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select"
android:layout_toRightOf="@id/previous"
android:layout_toLeftOf="@id/next" />
</RelativeLayout>
这应该有效。如果您包含“android:layout\u weight”,那么您应该将“android:layout\u width”或“android:layout\u height”作为“填充父项”
像这样修改代码
<LinearLayout
android:id="@+id/button_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Previous" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Select" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Next" />
ah,我还尝试为按钮指定textview的“android:singleLine=true”属性——这对出现类似问题的帖子没有帮助是的,我使用该帖子生成xml。但问题是,虽然它在某种程度上起作用,但我对包装文本有一个问题(请参见我问题的结尾部分)。我使用的这个0dip+layou-weight技巧是一个特别的技巧,正如我所了解的那样:)谷歌搜索,你会找到它的。是的,我想我可以在代码中实现这一点,但我只是在寻找一种用xml实现的方法:)我的意思是,如果您将上一个/下一个按钮设置为80dp宽度,并且您的文本大小在dp中定义,那么无论屏幕大小如何,文本都应该始终适合按钮。然后,您将知道您的两个按钮大小相同,选择按钮将占用其余空间,因为其他视图都没有设置布局权重。实际上,您可能仍然希望对所有按钮使用布局权重,只需将宽度设置为允许“previous”适合一行的最小值。问题是,考虑到将来可能翻译为其他语言,我不能依赖于知道文本的最小宽度。。。我更愿意找到一些方法让安卓发挥所有的魔力:)我试过了。此0dip技巧不适用于TableLayout。如果我没有它,按照你的建议去做,那么“下一个”和“上一个”按钮的宽度就不同了,我找不到使它们大小相同的方法:)试过了,但结果并不完全是我想要的:上一个和下一个按钮的宽度不同,虽然我希望下一步与上一步相同:)您可以手动设置按钮的宽度(答案更新),然后它们将是相同的宽度。我不知道这是否有帮助,也许你不想要一个固定的宽度。另外,你可以尝试另一种方法:设置中间按钮的宽度,并将其他两个按钮放置在其左右,将其宽度设置为填充。您需要更改布局以实现这一点,但要确保两个按钮的宽度相同。那么,问题是中央按钮的宽度:)是的,我开始认为这是唯一的方法,尽管不是完美的。与其指定固定宽度,我可以指定“minWidth”-应该更好:)不,这不起作用。通过这种方式,“选择”按钮甚至会从屏幕上消失,因为它试图将父对象的宽度匹配为其他两个的两倍;)我已经编辑了这篇文章。。这是减肥的问题。。现在它可以正常工作了。。。试试看!!你可以玩“android:layout_weight”哦,现在它工作得更好了,但不知怎么的,它最终还是和我原来遇到的问题一样。在这里,看一看:我已经编辑了发布的android:layout\u weight=“1.5”我想这就是你的确切答案!!!
<LinearLayout
android:id="@+id/button_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Previous" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Select" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Next" />