【问题标题】:How to stop button from getting scrunched?如何阻止按钮被挤压?
【发布时间】:2015-07-03 04:47:03
【问题描述】:

每当我的 TextView 中的数字变为两位数、三位数等时,我的按钮都会被按一下,我该如何做到这一点,以使按钮保持相同的大小并且不会被挤压?

   <LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:paddingRight="8dp"
    android:paddingLeft="8dp"
    android:layout_weight="1"
    android:gravity="center"

    >

    <Button
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:text="-"
        android:onClick="decrement"
        android:layout_weight="1"

        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="0"
        android:textSize="15sp"
        android:id="@+id/quantity_text_view"
        android:layout_weight="1"
        android:gravity="center"

        />

    <Button
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:text="+"
        android:onClick="increment"
        android:layout_weight="1"

        />

</LinearLayout>

【问题讨论】:

    标签: android button textview android-linearlayout


    【解决方案1】:

    仅将权重属性赋予中间 TextView 并将 wrap_content 设置为 Button :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingRight="8dp"
        android:paddingLeft="8dp"
        android:gravity="center">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:onClick="decrement"/>
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="15sp"
            android:id="@+id/quantity_text_view"
            android:layout_weight="1"
            android:gravity="center"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:onClick="increment"/>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-19
      • 2021-10-18
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多