MainActivity如下:
package cc.testcompounddrawables; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.TextView; /** * Demo描述: * 测试getCompoundDrawables()方法. * Returns drawables for the left, top, right, and bottom borders. * 该方法返回包含控件左,上,右,下四个位置的Drawable的数组 */ public class MainActivity extends Activity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mTextView=(TextView) findViewById(R.id.textView); Drawable [] drawables=mTextView.getCompoundDrawables(); for (int i = 0; i < drawables.length; i++) { Drawable drawable=drawables[i]; System.out.println("第"+i+"张图片 width="+drawable.getBounds().width()+ ",height="+drawable.getBounds().height()); } } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableBottom="@drawable/ic_launcher" android:drawableLeft="@drawable/ic_launcher" android:drawableRight="@drawable/ic_launcher" android:drawableTop="@drawable/ic_launcher" android:text="测试getCompoundDrawables()方法" android:gravity="center" android:layout_centerInParent="true" /> </RelativeLayout>