我有一张很酷的图片(花哨的圆角)。我想在我的 android 应用程序中实现该图像。单击它时,我希望它变暗一点(就像按下按钮一样)并启动一个新 Activity 。
我该怎么做以及我应该使用什么(ImageView、ImageButton 或只是 Button)?
最佳答案
您需要两个版本的炫酷图像,分别称为 active.png(单击状态,即变暗一点)和 inactive.png(正常状态)。
在drawable
文件夹为它们创建一个 Selector 文件,如下所示
喜欢:/drawable/cool_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/active"/>
<item android:drawable="@drawable/inactive"/>
</selector>
然后像这样使用 ImageButton:
<ImageButton
android:id="@+id/coolImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cool_button_selector"
android:background="@null" />
添加: android:background="@null"到 <ImageButton>
希望这有帮助:)
关于android - 如何使 ImageButton 仅显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12643736/