简体   繁体   English

Android:设置为按钮背景的图像没有缩小吗?

[英]Android: Image set as button background is not being shrunk?

I created a button with an image set as the background through a drawable. 我创建了一个按钮,其图像通过可绘制背景设置为背景。 When I run the program the image is not being centered nor shrunk to fit the button. 当我运行程序时,图像没有居中,也没有缩小以适合按钮。 But when you click it, the pressed image is showing properly. 但是,当您单击它时,按下的图像将正确显示。 This was working just the other day and I have no clue what happened to change it. 前几天这还是有效的,我不知道发生了什么改变。

main.xml main.xml

<Button
            android:id="@+id/rightarrow"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:clickable="false" />

Activity.java Activity.java

rightarrow = (Button) findViewById(R.id.rightarrow);
rightarrow.setBackgroundResource(R.drawable.rightarrow_drawable);

rightarrow_drawable.xml rightarrow_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/rightarrow_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/rightarrow" />
</selector>

Why not set the selector in XML? 为什么不以XML设置选择器? You do not have to set it by code. 您不必通过代码进行设置。 Try Project->Clean in Eclipse - maybe your resources got mixed. 在Eclipse中尝试Project-> Clean-可能您的资源混合了。

只需将您的Button替换为ImageView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM