我有ToggleButton
與設置在android:background
屬性中的圖像。此背景圖片適合所有背景。Android如何調整圖像背景以居中
我想在較大的ToggleButton中放置一個較小的背景圖像,以避免放大圖像以適應所有背景。
有人有線索嗎?
我有ToggleButton
與設置在android:background
屬性中的圖像。此背景圖片適合所有背景。Android如何調整圖像背景以居中
我想在較大的ToggleButton中放置一個較小的背景圖像,以避免放大圖像以適應所有背景。
有人有線索嗎?
好吧,我知道了:
首先,我們在我們的XML佈局創造我們的切換按鈕。想想我們的繪製是其中含有比ToggleButton
背景小兩(爲選中和未選中狀態)圖像的選擇:
<ToggleButton
android:id="@+id/messageSeeMoreToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff=""
android:textOn=""
android:background="@drawable/selector_wall_toggle_see_more_button"
android:contentDescription="@string/conversation_see_more_Button_description" />
然後,在我們的Activity
類中,我們發現我們的ToggleButton
像往常一樣:
viewHolder.messageSeeMoreToggleButton = (ToggleButton) view.findViewById(R.id.messageSeeMoreToggleButton);
現在,我們將使用StateListDrawable.setLevel
更改drawable的外觀。我們可以爲更好的結果調整setLevel
值:
StateListDrawable stateListDrawable = (StateListDrawable) viewHolder.messageSeeMoreToggleButton.getBackground();
stateListDrawable.setLevel(3000);
因此,我們將有ToggleButton
背景的正中央我們的形象,避免放大圖像。
我希望它可以幫助別人。
如果您想在圖片上放置文本,您可能必須使用代碼而不是XML來設置圖片。喜歡的東西:
Resources r = getResources();
Drawable myimage = r.getDrawable(R.drawable.myimage);
Button mybutton = findViewById(R.id.mybutton);
myimage.setBounds(0, 0, myimage.getIntrinsicWidth(), myimage.getIntrinsicHeight());
mybutton.setCompoundDrawables(myimage, null, null, null);