【问题标题】:Buttons shrinking when adding them to onClick?将按钮添加到 onClick 时按钮会缩小吗?
【发布时间】:2013-12-19 17:21:03
【问题描述】:

我在线性布局中添加了 3 个按钮,并在我的活动中为它们分配了一个 onClick 侦听器,其中两个在运行代码时收缩并仅包装包含的文本,

点击后第三个会缩小:

我正在使用的按钮:

我在 XML 中使用的代码:

<LinearLayout
            android:id="@+id/bottomButtons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/LivingRoom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ac_button_active"
                android:text="Living Room"
                android:textSize="20dp"
                android:textColor="#00CCCC"
                android:layout_marginTop="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="3dp"
                android:tag="pressed" />

            <Button
                android:id="@+id/NoAC"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ac_button_inactive"
                android:text="No AC"
                android:textSize="20dp"
                android:textColor="#00CCCC" 
                android:layout_marginTop="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="3dp"
                android:tag="transparent"/>

            <Button
                android:id="@+id/office"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ac_button_inactive"
                android:text="Office"
                android:textSize="20dp"
                android:textColor="#00CCCC" 
                android:layout_marginTop="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="3dp"
                android:tag="transparent"/>

【问题讨论】:

  • 你是在代码中修改视图吗?
  • 按钮的背景是 ColorDrawable 吗?你能发布所有按钮的@drawable/ac_button_inactive 和背景吗?
  • 您使用的是 9-patch 图像吗?
  • 尺寸变大了一点,但还是同样的问题
  • 问题可能出在您使用的背景可绘制对象上...

标签: android button background-image android-button


【解决方案1】:

当您使用 9-patch 图像作为背景时,它们将被缩小/调整以完全适合 Button 大小。按钮大小取决于文本大小和宽度。

所以如果需要,可以使用 Button 的填充来增加它的大小和边距。

<Button
    .......
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp" 
   ..........              />

【讨论】:

  • 给了我同样的结果
  • @MRefaat 尝试将 android:gravity="center" 用于按钮。
猜你喜欢
  • 1970-01-01
  • 2012-10-08
  • 2011-10-20
  • 2021-06-18
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多