RadioButton的表徵圖改變大小(TextView也適用),

來源:互聯網
上載者:User

RadioButton的表徵圖改變大小(TextView也適用),

RadioButton的表徵圖大小並沒有相應的布局參數,本文通過自訂屬性的方式自訂RadioButton,實現控製圖片大小。

  • 本文要點:
  •  問題如下:

 

  • 解決方案:

1.values/attrs.xml 檔案中:自訂rb_width  和  rb_height  兩個屬性

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyRadioButton">        <attr name="rb_width" format="dimension"/>        <attr name="rb_height" format="dimension"/>    </declare-styleable></resources>

 

2.自訂RadioButton

 1 package top.toly.www.myqq.view; 2  3 import android.content.Context; 4 import android.content.res.TypedArray; 5 import android.graphics.drawable.Drawable; 6 import android.util.AttributeSet; 7 import android.widget.RadioButton; 8  9 import top.toly.www.myqq.R;10 import utils.shortUtils.Change;11 12 /**13  * 作者:張風捷特烈14  * 時間:2018/3/28:6:3015  * 郵箱:1981462002@qq.com16  * 說明:自訂RadioButton 屬性:rb_width  rb_height17  */18 public class MyRadioButton extends RadioButton {19 20     private float mImg_width;21     private float mImg_height;22 23     public MyRadioButton(Context context) {24         super(context);25     }26 27     public MyRadioButton(Context context, AttributeSet attrs) {28         super(context, attrs);29         TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);30         mImg_width = t.getDimension(R.styleable.MyRadioButton_rb_width, Change.dp2px(25));31         mImg_height = t.getDimension(R.styleable.MyRadioButton_rb_height, Change.dp2px(25));32         t.recycle();33     }34 35     @Override36     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {37         super.onMeasure(widthMeasureSpec, heightMeasureSpec);38         //讓RadioButton的表徵圖可調大小 屬性:39         Drawable drawableLeft = this.getCompoundDrawables()[0];//獲得文字左側圖片40         Drawable drawableTop = this.getCompoundDrawables()[1];//獲得文字頂部圖片41         Drawable drawableRight = this.getCompoundDrawables()[2];//獲得文字右側圖片42         Drawable drawableBottom = this.getCompoundDrawables()[3];//獲得文字底部圖片43         if (drawableLeft != null) {44             drawableLeft.setBounds(0, 0, (int) mImg_width, (int) mImg_height);45             this.setCompoundDrawables(drawableLeft, null, null, null);46         }47         if (drawableRight != null) {48             drawableRight.setBounds(0, 0, (int) mImg_width, (int) mImg_height);49             this.setCompoundDrawables(null, null, drawableRight, null);50         }51         if (drawableTop != null) {52             drawableTop.setBounds(0, 0, (int) mImg_width, (int) mImg_height);53             this.setCompoundDrawables(null, drawableTop, null, null);54         }55         if (drawableBottom != null) {56             drawableBottom.setBounds(0, 0, (int) mImg_width, (int) mImg_height);57             this.setCompoundDrawables(null, null, null, drawableBottom);58         }59     }60 }

 

3.使用屬性控制RadioButton 中的圖片大小

  • 注意:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:toly="http://schemas.android.com/apk/res-auto"    android:id="@+id/my_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/qq_title_text">    <RadioGroup        android:id="@+id/rg_btns"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal">        <top.toly.www.myqq.view.MyRadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/rb_msg"            android:checked="false"            toly:rb_width="30dp"            toly:rb_height="30dp"            android:button="@null"            android:gravity="center"            android:drawableTop="@drawable/tab_msg_selector"            android:text="訊息"/>        <top.toly.www.myqq.view.MyRadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/rb_contact"            android:background="@android:color/transparent"            android:checked="false"            android:button="@null"            toly:rb_width="30dp"            toly:rb_height="30dp"            android:gravity="center"            android:drawableTop="@drawable/tab_contact_selector"            android:text="連絡人"/>        <top.toly.www.myqq.view.MyRadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/rb_act"            android:button="@null"            toly:rb_width="30dp"            toly:rb_height="30dp"            android:gravity="center"            android:background="@android:color/transparent"            android:drawableTop="@drawable/tab_act_selector"            android:text="動態"           />    </RadioGroup></RelativeLayout>

 

4.結果圖:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.