如何在Android布局中实现Button的自动宽度 [英] How to achieve automatic width of Button in Android layout
问题描述
我有一些垂直列出的按钮,我需要所有按钮都具有相同的宽度,而且还要在其中显示所有文本.
I have a few Button listed vertically and I need all of them to have the same width, but also to display all text inside.
基本上,我都需要宽度作为最大宽度的包装宽度.
Basically I need width for all of them as a wrapped width of the largest one.
希望我解释得很好.
现在...我已经有一种布局可以在我的三星Galaxy S2(4.1.2)上使用,但是在朋友的电话上-三星GT-N7100(note2)和android 4.4.2-它无法正常工作-有些文本不会显示在按钮中.
Now... I already have one layout that is working on my Samsung Galaxy S2 (4.1.2), but on friend's phone - Samsung GT-N7100 (note2) and android 4.4.2 - it is not working - some text is not displayed in the Button.
这是我的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="word"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:textSize="30sp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="@+id/word"/>
<TextView
android:text="14/36\nM: 1"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:id="@+id/score"
android:layout_toRightOf="@+id/word"
android:gravity="right|center"
android:paddingRight="10dp"/>
<LinearLayout
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/adapter">
<Button
android:text="1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt1"/>
<Button
android:text="2"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt2" />
<Button
android:text="3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt3" />
<Button
android:text="4"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt4" />
<Button
android:text="5"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt5" />
</LinearLayout>
<Button
android:text="Next"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:id="@+id/next"/>
</RelativeLayout>
推荐答案
我仅使用XML(无代码)来完成此操作-对于两个TextView.但是同样的技术也应该适用于任意数量的按钮.
I have done this using XML only (no code) - for two TextViews. But the same technique should work for arbitrary number of buttons as well.
- 假设我们有两个TextViews A和B.
- 将每个TextView包裹在LinearLayout(内部仅包含一个元素)中-La和Lb.
- 将每个TextView(A,B)的宽度设置为'wrap_content',将背景设置为透明
- 将LinearLayouts(La,Lb)宽度设置为"match_parent",将背景设置为所需的任何颜色/形状
- 将La和Lb包裹在另一个Vertical LinearLayout容器L中,将L的宽度/高度设置为"wrap_content",将高度设置为"wrap_content"
- 在每个TextView的LinearLayout(La,Lb)上设置填充以使其看起来不错
- 在(La,Lb)上设置边距以创建间距
- 应该这样做.
注意:请确保每个TextView的背景颜色/形状是通过单个布局容器的背景设置的,而不是通过TextView本身设置的.
- Say we have two TextViews A and B.
- Wrap each TextView in a LinearLayout (with only one element inside) - La and Lb.
- Set each TextView (A,B) width to 'wrap_content', set background transparent
- Set the LinearLayouts (La,Lb) widths to 'match_parent', set background to whatever color/shape you want
- Wrap La and Lb inside another Vertical LinearLayout container L, set the width/height of L to be 'wrap_content', height to 'wrap_content'
- set padding on each TextView's LinearLayout (La, Lb) to make it look good
- set margins on (La, Lb) to create spacing
- That should do it.
Note: Make sure the background color/shape for each TextView is set via its background of individual layout container and not on the TextView itself.
我知道这个问题是3年前提出的-但是我认为在其他地方,仅涉及XML的解决方案也存在相同/相似的问题.因此希望这对以后的人有所帮助.
I know this question was asked 3 years ago - but I think I saw same/similar questions elsewhere for a solution that involves XML only. So hope this helps someone in the future.
这篇关于如何在Android布局中实现Button的自动宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!