0

I added an image called "star" to a button and the picture appeared, but it made the button larger.

<Button
android:id="@+id/latest_photoB"
android:layout_height="87dp"
android:layout_weight="1"
android:background="#dcdcdc"
android:drawableTop="@drawable/star"
android:text="Latest Photos"
android:textSize="10dp"
android:textColor="#000000"  />

I tried many different ways, such as making it an imagebutton.

I tried android:background = image, but nothing worked. How can I add a picture, and keep it the same size?

4

5 回答 5

1

如果您希望按钮具有图像背景,请执行以下操作:

<Button
    android:id="@+id/latest_photoB"
    android:layout_height="87dp"
    android:layout_width="87dp"
    android:background="@drawable/star"
    android:text="Latest Photos"
    android:textSize="10dp"
    android:textColor="#000000"  />

如果按钮应该是固定大小的,那么 layout_weight 不是必需的。此外,您还没有定义layout_weight属性。我已经设置好了87dp。根据需要更改它。

于 2013-08-16T17:46:04.633 回答
0

按钮的默认背景可绘制使用所谓的九补丁。它将自己调整为按钮内容的大小。按钮是一个特殊的 TextView,它可以在其 4 个侧面的每一侧附加可绘制对象(复合可绘制对象)。文本和复合可绘制对象构成了按钮的内容,这就是按钮调整大小的原因。

复合drawables是对视图层次结构的优化,因为它减少了非常常见场景的视图数量,但听起来你想要一个ImageView +一个按钮,当你点击它时,ImageView的响应方式与按钮相同?

在这种情况下,将按钮包装在一个带有 ImageView 的布局中,并将 onClickListener 设置在布局上而不是按钮上

于 2013-08-16T17:39:56.857 回答
0

摆脱:

android:layout_weight="1"

相反,为您的按钮定义一个特定的大小:

android:layout_height="87dp"
android:layout_width="50dp"

希望这可以帮助。

于 2013-08-16T17:42:50.147 回答
0

也许你正在寻找android:setAdjustViewBounds = "false"

于 2013-08-16T17:33:52.000 回答
0

如果您不设置 Button 的高度和宽度,Android Button 将包装内容(与图像大小相同)。试试这个方法

<Button
android:id="@+id/latest_photoB"
android:layout_height="87dp"
android:layout_width="87dp"
android:layout_weight="1"
android:background="@drawable/star"
android:text="Latest Photos"
android:textSize="10dp"
android:textColor="#000000"  />
于 2013-08-16T17:46:53.503 回答