Drawable類,在不同解析度下的設定
阿新 • • 發佈:2018-12-23
手工設定文字與圖片相對位置時,常用到如下方法:
setCompoundDrawables(left, top, right, bottom);
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
意思是設定Drawable顯示在text的左、上、右、下位置。
(Textview、Button都可以)
但是兩者有些區別:
//getIntrinsicWidth()取得的是Drawable在手機上的寬度,所以不同解析度下獲取到的值是不同的,關鍵所在處
setCompoundDrawables(left, top, right, bottom);
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
意思是設定Drawable顯示在text的左、上、右、下位置。
(Textview、Button都可以)
但是兩者有些區別:
setCompoundDrawables 畫的drawable的寬高是按drawable.setBound()設定的寬高,所以才有The Drawables must already have had setBounds(Rect) called.意思是說使用之前必須使用Drawable.setBounds設定Drawable的長寬。 |
1、獲取資原始檔夾圖片的Drawable物件
this.getResources().getDrawable(R.drawable.icon)
2、獲取與設定Drawable的長和寬
RadioButton rr =(RadioButton)this.findViewById(R.id.radio_button0);
Drawable d1 = rr.getCompoundDrawables()[1]; //此處取的是android:drawableTop的圖片
drawable.setBounds(0, 0, d1.getIntrinsicWidth(), d1.getIntrinsicHeight());