Android 如何在Imagebutton上发挥作用单击

Android 如何在Imagebutton上发挥作用单击,android,xml,imagebutton,Android,Xml,Imagebutton,我使用下面的代码设计圆形按钮,然后我想在按钮上显示一些文本,所以我使用框架布局,然后在按钮上放置一些文本。 现在的挑战是我的按钮仍然是。它不会显示任何点击效果 其中drawable和src分别用于圆形按钮和图像按钮 现在,我的按钮不会对设计产生任何影响,只要单击我是如何产生这种效果的 <FrameLayout> <ImageButton android:id="@+id/btn_profile_view" android:layout_width="70dp" android:

我使用下面的代码设计圆形按钮,然后我想在按钮上显示一些文本,所以我使用框架布局,然后在按钮上放置一些文本。 现在的挑战是我的按钮仍然是。它不会显示任何点击效果 其中drawable和src分别用于圆形按钮和图像按钮

现在,我的按钮不会对设计产生任何影响,只要单击我是如何产生这种效果的

<FrameLayout>
<ImageButton
android:id="@+id/btn_profile_view"
android:layout_width="70dp"
android:layout_height="67dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:onClick="compareButtonClick"
android:background="@drawable/roundedbutton"
android:src="@drawable/ic_action_person" />
<TextView
android:layout_width="45dp"
android:layout_height="20dp"
android:layout_gravity="bottom"
android:layout_marginLeft="38dp"
android:clickable="false"
android:text="@string/profile"
android:textColor="@color/btn_txt" >
</TextView>
</FrameLayout>


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#D3D3D3" />

<corners
    android:bottomLeftRadius="8dip"
    android:bottomRightRadius="8dip"
    android:topLeftRadius="8dip"
    android:topRightRadius="8dip" />

在这里,您将得到您的点击回拨,您不需要在任何地方设置点击侦听器

public void compareButtonClick(View v){
    Toast.makeText(this, "Image Button Click", Toast.LENGTH_SHORT).show();
}

这可能会有所帮助,但您必须修改它以满足您的需要

1在drawable文件夹中创建roundedbutton.xml

3将button_normal.xml添加为按钮的背景

android:background="@drawable/button_normal"

创建一个具有不同可绘制项的选择器,以处理不同的状态;启用、禁用、单击、聚焦等

为什么您没有指定framelayout维度属性?并显示您的代码,在哪里尝试获取onclick回调我不需要框架布局属性,因为我将框架布局放在表布局中,表行布局,我使用表行中的权重属性在单行上显示3个图像按钮。jave代码是juct onclicklistner从一个活动移动到另一个..在每个按钮上
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/roundedbutton" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/ic_launcher" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/ic_launcher" android:state_focused="true"/>
    <item android:drawable="@drawable/roundedbutton" android:state_focused="false" android:state_pressed="false"/>

    <!-- 2 and 3 pressed and selected -->


</selector>
android:background="@drawable/button_normal"