Android 单击“在图像中设置背景色”按钮

Android 单击“在图像中设置背景色”按钮,android,webview,onclick,Android,Webview,Onclick,这是我的档案。我想在单击图像按钮时更改其背景色,以便用户可以看到它被单击,因为当我单击它时,它似乎没有被单击,但它正在工作。我可以看到在其他问题上,您需要一个drawable来完成它,但我不知道我将在drawable文件夹中的xml文件中放置什么。你能帮帮我吗?多谢各位 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

这是我的档案。我想在单击图像按钮时更改其背景色,以便用户可以看到它被单击,因为当我单击它时,它似乎没有被单击,但它正在工作。我可以看到在其他问题上,您需要一个drawable来完成它,但我不知道我将在drawable文件夹中的xml文件中放置什么。你能帮帮我吗?多谢各位

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
    android:id="@+id/webViewHeaderArea"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="@android:color/white">

    <ImageButton
        android:id="@+id/btnCloseWebView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/close"
        android:background="@null"
        android:contentDescription="@string/button" />

    <ImageButton
        android:id="@+id/btnReload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btnCloseWebView"
        android:src="@drawable/reload"
        android:background="@null"
        android:contentDescription="@string/button" />

    <ImageButton
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"
        android:src="@drawable/back"
        android:background="@null"
        android:contentDescription="@string/button" />

    <ImageButton
        android:id="@+id/btnStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="17dp"
        android:layout_toRightOf="@+id/btnBack"
        android:src="@drawable/stop"
        android:background="@null"
        android:contentDescription="@string/button" />

    <ImageButton
        android:id="@+id/btnForward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/btnStop"
        android:src="@drawable/forward"
        android:background="@null"
        android:contentDescription="@string/button" />

</RelativeLayout>

    <WebView android:id="@+id/web_engine"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" />


您需要在名为contact_bg.xml的可绘图文件夹中创建一个xml文件

然后,你必须像下面这样写

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

将其设置为图像按钮中的图像源

并将两个文件放入名为contact.png和contact_hover.png的可绘制hdpi中


希望,这将有所帮助

您可以使用选择器进行此操作。可以使用可绘制图像,也可以为每个状态定义自己的形状。你可以做这样的事情

<item android:state_pressed="true" >
    <shape>
        <gradient
       <!-- set values here -->
            />
        <stroke
       <!-- set values here -->
             />
        <corners
      <!-- set values here -->
             />
        <padding
      <!-- set values here -->
            />
    </shape>
</item>


/>
/>

另请查看此

查看滚动到自定义背景我应该在这里调用contact_bg吗?android:src=“@drawable/contact_bg”在图像按钮中设置此选项