【问题标题】:Button layout_height=wrap_content inside linear layout has height much bigger then wrap_content线性布局内的按钮 layout_height=wrap_content 的高度比 wrap_content 大得多
【发布时间】:2014-03-17 14:52:25
【问题描述】:

我有带有 TextView 和 Button 的 LinearLayout。我将 layout_height 属性设置为 wrap_content 但 layout_height 的尺寸要大得多。如果我删除此按钮,那么一切都很好。我尝试减小按钮文本大小,但按钮的框架仍然相同。我不想要硬编码按钮大小。这个奇怪的结果可能是什么原因?

 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="1" >

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="@dimen/left_margin"
                    android:layout_marginTop="@dimen/top_margin"
                    android:layout_weight=".3"
                    android:text="@string/contact" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.7"
                    android:layout_marginLeft="@dimen/left_margin"
                    android:layout_marginTop="@dimen/top_margin"
                    android:orientation="horizontal"
                    android:weightSum="1" >

                    <TextView
                        android:id="@+id/contact"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent" 
                        android:gravity="center_vertical"/>

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"                                                                
                        android:text="@string/contact" android:textSize="@dimen/button_font_size"/>

                </LinearLayout> 

【问题讨论】:

    标签: android android-layout button android-linearlayout


    【解决方案1】:

    Button 具有背景和其他布局属性,似乎无法降低其高度。一种选择是使用具有自定义样式的TextView 并处理单击事件,使其行为类似于Button

    例子:

    <TextView
        android:layout_height="wrap_content"
        android:background="#ddd"
        android:padding="6dp"
        android:layout_width="wrap_content"
        android:text="Contact" />
    

    正确设置TextView 的样式需要一些工作和时间,但如果您别无选择,它可能会为您解决问题。

    【讨论】:

      【解决方案2】:

      我在使用按钮时也遇到了这个问题,您可以手动设置高度和宽度,但我也没有找到减小 wrap_content 大小的固定方法。

      android:layout_width="40dp"
      android:layout_height="30dp"
      

      【讨论】:

        【解决方案3】:

        这是因为按钮的最小高度。设置android:minHeight="1dp" 或任何其他大小以实现它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-24
          • 2019-11-12
          • 1970-01-01
          • 2011-08-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多