Android developers code from the Set Drawable picture does not show the problem

Problem Description

  We get Drawable will find that when you set to View, the picture does not show the problem code such as the following code:

        Drawable drawable = getResources().getDrawable(R.drawable.bg_btn_green, null);
        btn1.setCompoundDrawables(null, drawable, null, null);

problem causes

  Code in the above manner drawable actually acquired is not provided the setBounds () Size

Solve the problem

  Provided drawable to the setBounds () on the line, as follows:

drawable.setBounds(0, 0, 100, 100);

  However, this does not solve the problem of adaptation size, because it is a fixed value set your own. Here are two ideas to fit size

  If your first drawable xml file is a vector (vector usually contains width and height) or comprises internal dimensions, such as the background provided with a xml android:. Width = "100dp" and android: height = "100dp " :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorGreen4" />
    <corners android:radius="5dp"/>
    <size android:width="100dp"
        android:height="100dp"/>
</shape>

  Then you can choose the following ways adaptation size:

        Drawable drawable = getResources().getDrawable(R.drawable.bg_btn_green, null);
//        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        btn1.setCompoundDrawables(null, drawable, null, null);

drawable.getIntrinsicWidth (), drawable.getIntrinsicHeight () Gets the internal width and height

drawable.getMinimumWidth (), drawable.getMinimumHeight () Gets the recommended minimum width and height

  Second. Drawable no internal dimensions

  Drawable without internal size is typically a key background or a background of chat box, or a dividing line again this time we only need adaptation size View on the line following code:

btn1.post ( new new Runnable () { // because when View is created in the onCreate life cycle is not measured size, so we need to be processed into Drawable View lined up in 
            @Override
             public  void RUN () { 
                the drawable drawable . getResources = () getDrawable (R.drawable.bg_btn_green, null );
                 int width = 0 ;
                 int height = 0 ;
                 IF (drawable.getIntrinsicWidth () == -1) { // If it shows -1 no width value 
                    width = btn1.getWidth (); // Get the width of View 
                } the else {
                    width = drawable.getIntrinsicWidth();
                }
                if (drawable.getIntrinsicHeight() == -1){
                    height = btn1.getHeight();
                }else {
                    height = drawable.getIntrinsicHeight();
                }
                drawable.setBounds(0, 0, width, height);
                btn1.setBackground(drawable);
            }
        });

If drawable.getMinimumWidth (), drawable.getMinimumHeight () value is determined to become zero. Since this method in a comment 2 also shows the recommended minimum if not returns 0

 

Guess you like

Origin www.cnblogs.com/guanxinjing/p/11249427.html