android用户界面上未显示按钮点击效果(悬停)
由于我需要圆形按钮,我使用以下代码 这是layout.xml文件中的我的按钮android用户界面上未显示按钮点击效果(悬停),android,Android,由于我需要圆形按钮,我使用以下代码 这是layout.xml文件中的我的按钮 <RoundedImageView android:id="@+id/btnCheck" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher" /> 在我的MainActivity类中,我声明了一个ImageVi
<RoundedImageView
android:id="@+id/btnCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher" />
在我的MainActivity类中,我声明了一个ImageView:-
private ImageView imageBtnCheck;
在OnCreate中,我有下面的代码,并检查_图标是否为drawable hdpi文件夹中的.png文件
imageBtnCheck = (ImageView) findViewById(R.id.btnCheck);
Bitmap iconCheck = BitmapFactory.decodeResource(getResources(),R.drawable.check_icon);
imageBtnCheck.setImageBitmap(iconCheck);
Onclick代码如下所示,我正在onCreate中调用wireleventforbtncheck:-
private void wireEventForBtnCheck() {
imageBtnCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//Code here
}
});
}
通过这段代码,我可以根据需要显示带有图标的圆形按钮。而且当我点击按钮时,点击事件也在运行。但UI中的点击效果并不像普通android按钮那样出现
我是否添加了任何属性或任何类型的代码来显示动态加载按钮的单击效果?当您设置可绘制的imagebutton悬停效果时。因此,您必须使用图像按钮的选择器。例如,在drawable文件夹中创建选择器文件 ImageButtonSelector.xml 然后将此选择器设置为图像按钮源
<RoundedImageView
android:id="@+id/btnCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ImageButtonSelector" />
您可以通过可绘制的xml来实现这一点
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/ic_launcher_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/ic_launcher_pressed"/>
<item android:state_focused="true" android:drawable="@drawable/ic_launcher_selected"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/ic_launcher_default"/>
</selector>
按钮在哪里?我可以看到ImgeviewYou已经覆盖了最初处理标准行为绘图的onDraw方法。与尝试修改ImageView相比,更好的方法是修改您放入其中的可绘制内容。请参阅。*standard behavior=background drawable,src drawable,state list selector,elevation shadow等。谢谢,我将尝试此操作。如果在xml文件中设置图像按钮背景源后,再次在java文件中设置图像按钮背景源,则它不起作用,不要在选择器中的java set images中设置背景源,这样它就会工作;在oncreate方法中。
<RoundedImageView
android:id="@+id/btnCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ImageButtonSelector" />
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/ic_launcher_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/ic_launcher_pressed"/>
<item android:state_focused="true" android:drawable="@drawable/ic_launcher_selected"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/ic_launcher_default"/>
</selector>
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable .addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.ic_launcher_pressed));
stateListDrawable .addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.ic_launcher_selected));
stateListDrawable .addState(new int[] {},
getResources().getDrawable(R.drawable.ic_launcher_default));
imageBtnCheck.setImageDrawable(stateListDrawable);