RadioButton的圖片大小設置

在RadioButton使用中,可能需要放入圖片,但是XML中無法直接設置圖片的大小。

<RadioButton
    android:gravity="center"
    android:id="@+id/rb_first"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:drawableTop="@drawable/selector_ic_first"
    android:text="首頁"/>
<RadioButton
    android:gravity="center"
    android:id="@+id/rb_search"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:drawableTop="@drawable/selector_ic_search"
    android:text="查詢"/>
<RadioButton
    android:gravity="center"
    android:id="@+id/rb_me"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:drawableTop="@drawable/selector_ic_people"
    android:text="我的"/>
效果圖:可以發現圖片太大了


如何設置圖片大小:

Java代碼實現:

初始化使用ButteerKnife插件

@BindView(R.id.rb_first)
RadioButton rbFirst;
@BindView(R.id.rb_search)
RadioButton rbSearch;
@BindView(R.id.rb_me)
RadioButton rbMe;

private void changeImageSize() {
    //定義底部標籤圖片大小
    Drawable drawableFirst = getResources().getDrawable(R.drawable.selector_ic_first);
    drawableFirst.setBounds(0, 0, 69, 69);//第一0是距左右邊距離,第二0是距上下邊距離,第三69長度,第四寬度
    rbFirst.setCompoundDrawables(null, drawableFirst, null, null);//只放上面

    Drawable drawableSearch = getResources().getDrawable(R.drawable.selector_ic_search);
    drawableSearch.setBounds(0, 0, 69, 69);//第一0是距左右邊距離,第二0是距上下邊距離,第三69長度,第四寬度
    rbSearch.setCompoundDrawables(null, drawableSearch, null, null);//只放上面

    Drawable drawableMe = getResources().getDrawable(R.drawable.selector_ic_people);
    drawableMe.setBounds(0, 0, 69, 69);//第一0是距左右邊距離,第二0是距上下邊距離,第三69長度,第四寬度
    rbMe.setCompoundDrawables(null, drawableMe, null, null);//只放上面
}
效果圖:設置大小後,圖片變小了





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章