【发布时间】:2011-10-10 06:52:51
【问题描述】:
这应该很容易,我真的认为我所做的是正确的,但显然不是。我有一个我想用作按钮的图像,我已经创建并工作了按钮。但现在我只想显示这张图片,而不是标准的 android“按钮”。
有人知道怎么做吗?
【问题讨论】:
这应该很容易,我真的认为我所做的是正确的,但显然不是。我有一个我想用作按钮的图像,我已经创建并工作了按钮。但现在我只想显示这张图片,而不是标准的 android“按钮”。
有人知道怎么做吗?
【问题讨论】:
使用<selector
在您的drawable-hdpi 文件夹中创建名为button_image.xml 的xml 文件
并添加以下行:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/picture" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/picture_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/picture_pressed" />
<item android:drawable="@drawable/picture" />
</selector>
然后在您声明按钮的主 xml 中添加这一行
android:background="@drawable/button_image"
然后你会得到真正的按钮效果。请注意,您需要两张图片才能获得按钮被点击的效果。
如果您不想使用选择器而只使用图片,那么只需
android:background="@drawable/myimage"
【讨论】:
使用ImageButton 类。
【讨论】:
您可以使用ImageButtons 代替常规按钮
只需在 XML 中从图像中设置背景
<ImageButton android:background="@drawable/mypicture"/>
一个不错的教程是Here
【讨论】:
跟我来,你可以在android中使用很多控件来解决这个问题(Button、ImageView、ImageButton,甚至是TextView ...)。只需将图片设置为背景,然后实现 OnClickListener 来处理点击事件。
例如,使用 ImageView, - 在布局文件中:
<ImageView android:id="@+id/button_login" android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/button_login" />
在后台代码文件中:
ImageView login;
login = (ImageView) findViewById(R.id.button_login);
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO: your code here
}
});
【讨论】:
您需要在按钮处将android:background="@drawable/my_pic" 写入您的xml 文件。其中 my_pic 是您的图片名称。希望对你有帮助。
【讨论】: