有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将主题设置为具有自定义绘图功能的按钮

我有一个应用程序,其中我使用自定义绘图作为按钮背景。 然而,我并没有为聚焦、选定等状态创建单独的绘图。

我的问题是,如果不定义额外的绘图功能,是否可以使用默认Android突出显示按钮。全息彩色(蓝色)


共 (1) 个答案

  1. # 1 楼答案

    您可以使用选择器(状态列表),然后将其指定给布局中的视图,以修改视图在不同事件期间的行为方式

    因此选择器(我们称之为myselector)可能如下所示:

    <?xml version="1.0" enconding "utf-8">
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
         //When the view gets pressed the drawable gets set. 
         //You could also use android:color
         <item android:state_pressed="true"
               android:drawable="<Your drawable>"
    
         <item android:state_focused="true"
               android:drawable="<Another drawable>">
    
     </selector>
    

    然后在布局中为按钮设置选择器(如果需要,也可以在代码中设置):

    <Button
      android:id="@+id/mybutton"
      android:text="Click me!"
      android:background="@drawable/myselector" //Here the selector is set
    />
    

    应该是这样。您可以阅读选择器/状态列表here

    希望这有帮助