【问题标题】:How to achieve automatic width of Button in Android layoutAndroid布局中如何实现Button的自动宽度
【发布时间】:2015-04-22 18:40:12
【问题描述】:

我有几个 Button 垂直列出,我需要它们都具有相同的宽度,而且要显示里面的所有文本。

基本上我需要它们的宽度作为最大宽度的包裹宽度。

希望我解释清楚。

现在...我已经有一种布局可以在我的三星 Galaxy S2 (4.1.2) 上运行,但在朋友的手机上 - 三星 GT-N7100 (note2) 和 android 4.4.2 - 它不工作 - 有些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>

【问题讨论】:

  • 出现了什么问题?可以发个截图吗?

标签: android layout widget android-linearlayout


【解决方案1】:

尝试设置 android.width="auto"

【讨论】:

    【解决方案2】:

    您可以扩展 LinearLayout 以强制其所有子级具有最大宽度:

    // LinearLayoutButtonsHolder.java
    package github.yaa110.widget;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.LinearLayout;
    
    public class LinearLayoutButtonsHolder extends LinearLayout {
    
        public LinearLayoutButtonsHolder(Context context) {
            super(context);
        }
    
        public LinearLayoutButtonsHolder(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public LinearLayoutButtonsHolder(Context context, AttributeSet attrs,
                                         int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
    
            if (getChildCount() == 0) return;
    
            getChildAt(getChildCount() - 1).addOnLayoutChangeListener(new OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    v.removeOnLayoutChangeListener(this);
    
                    for (int i = 0; i < getChildCount(); i ++) {
    
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT,
                                getChildAt(i).getMeasuredHeight());
    
                        getChildAt(i).setLayoutParams(lp);
                    }
                }
            });
        }
    
    }
    

    现在在布局的 xml 文件中,使用 LinearLayoutButtonsHolder 而不是 LinearLayout不要忘记将每个Buttonandroid:layout_width设置为wrap_content

    <github.yaa110.widget.LinearLayoutButtonsHolder
        android:id="@+id/adapter"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content">
    
        <Button
            android:text="Long text for test"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:id="@+id/opt1"/>
    
        <Button
            android:text="2"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:id="@+id/opt2" />
    
        <Button
            android:text="3"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:id="@+id/opt3" />
    
        <Button
            android:text="4"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:id="@+id/opt4" />
    
        <Button
            android:text="5"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:id="@+id/opt5" />
    
    </github.yaa110.widget.LinearLayoutButtonsHolder>
    

    【讨论】:

      【解决方案3】:

      像这样使用线性布局

          <LinearLayout
              android:layout_centerInParent="true"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginLeft="20dp"
              android:layout_marginRight="20dp"
              android:id="@+id/adapter">
      

      【讨论】:

        【解决方案4】:

        我仅使用 XML(无代码)完成了这项工作 - 用于两个 TextView。但同样的技术也适用于任意数量的按钮。

        • 假设我们有两个 TextView 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 本身上设置的。

        我知道这个问题是 3 年前提出的 - 但我想我在其他地方看到了相同/类似的问题,用于仅涉及 XML 的解决方案。所以希望这对将来的人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-12
          • 2023-03-08
          • 1970-01-01
          相关资源
          最近更新 更多