TextView于getCompoundDrawables()使用演示样本的方法

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>

版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/lcchuguo/p/4799018.html