![](/img/trans.png)
[英]Hello , How can I get the name or ID of a Button DrawableTop Icon?
[英]How can I have reference for drawableTop of a Button?
我在我的 Button 中聲明了一個可繪制對象,我想獲得一個參考,因為我創建了一個顯示對話框的方法,提取該按鈕的文本和可繪制對象,並使用它來動態更改對話框的文本和圖標。
請檢查此代碼段:
private fun showProductDialog(button : Button){
//extract product info selected from button
//show dialog
val dialog = Dialog(this)
dialog.setContentView(R.layout.dialog_product)
//set text and icon
val titleLayout = dialog.findViewById<LinearLayout>(R.id.ll_product_label)
val titleText = titleLayout.findViewById<HelveticaBoldTextView>(R.id.tv_category_click)
val titleIcon = titleLayout.findViewById<ImageView>(R.id.iv_product_icon)
//get text of button, set to titleText
titleText.text = button.text.toString().uppercase()
//extract drawableTop of Button then
//set image for titleIcon = drawable in Button
dialog.show()
}
要獲得 DrawableTop 你可以使用這個
Drawable[] drawables = button.getCompoundDrawables();
Drawable d = drawables[1];
要設置 DrawableTop 你可以使用這個
button.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon, 0, 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.