gpt4 book ai didi

点击时Android突出显示图像按钮

转载 作者:IT老高 更新时间:2023-10-28 23:27:18 26 4
gpt4 key购买 nike

我正在使用 ImageButton。但是单击时我没有突出显示。我用谷歌搜索,许多人建议在显示另一个图像的地方使用选择器。有没有办法解决。通过仅使用一张图像并突出显示它或赋予它发光效果。以便用户知道该按钮已被点击。

最佳答案

这实际上并不难做到。您甚至不需要创建 2 个单独的 .png 文件或类似的文件。例如,如果您想要一个具有渐变的按钮,然后在按下按钮时更改它:

第 1 步:创建默认按钮渐变(drawable/default_button.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<gradient android:endColor="#8ba0bb" android:startColor="#43708f" android:angle="90" />
<stroke android:width="1dp" android:color="#33364252" />
</shape>

第 2 步:创建默认按钮按下渐变(drawable/default_button_pressed.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<gradient android:endColor="#43708f" android:startColor="#8ba0bb" android:angle="90" />
<stroke android:width="1dp" android:color="#33364252" />
</shape>

第三步:创建选择器(drawable/default_button_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/default_button_pressed" />
<item android:drawable="@drawable/default_button" />
</selector>

第四步(可选):为按钮创建样式(values/style.xml):

<resources>
<style name="DefaultButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/default_button_selector</item>
</style>
</resources>

第五步:使用按钮(layout/main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent">

<button style="@style/DefaultButton" />

</RelativeLayout>

如您所见,这并不是特别难做到。

关于点击时Android突出显示图像按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5327553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com