[英]How to set ImageButton size and when button is pressed show it on added image
[英]How to show that ImageButton is pressed?
如果我向其中添加圖像,如何顯示ImageButton被按下:
Drawable testPic = getResources().getDrawable(R.drawable.test_pic);
button.setImageDrawable( testPic );
並設置透明背景,以便只有圖像:
button.setBackgroundColor(Color.TRANSPARENT);
因此,當我按下圖像按鈕時,我看不到它被按下了。 我希望該圖像在按下時會突出顯示。
另外,我的每個按鈕都是通過代碼動態創建的,我不知道它將是什么圖像以及有多少個按鈕。
有什么想法嗎?
謝謝。
您應該使用選擇器來選擇UI元素的所有不同外觀,包括ImageButton
。
首先是state_Pressed=true
,這表示按下時的樣子。 之后是常規狀態,這是按鈕的正常狀態。
我認為您應該使用狀態列表 ...
您必須在運行時更改Image。 您可以借助xml文件來實現這些目標。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/category_listing_bg_img" />
<item android:state_pressed="true" android:drawable="@drawable/category_listing_bg_img_pressed" />
</selector>
將此xml復制到您的可繪制文件夾中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.