View.setBackgroundResource(int resId)當resId代表的Drawable帶有Padding時,會改變View本身的Padding值

cxc19890214發表於2016-12-06

View.setBackgroundResource(int resId)原始碼如下:



    /**
     * Set the background to a given Drawable, or remove the background. If the
     * background has padding, this View's padding is set to the background's
     * padding. However, when a background is removed, this View's padding isn't
     * touched. If setting the padding is desired, please use
     * {@link #setPadding(int, int, int, int)}.
     *
     * @param background The Drawable to use as the background, or null to remove the
     *        background
     */


現在驗證如下:

1.Activity 佈局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFF00"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/test_tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/test_drawable_with_padding"
        android:gravity="center"
        android:padding="20dp"
        android:text="HelloWorld" />

    <TextView
        android:id="@+id/test_tv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:padding="20dp"
        android:text="HelloWorld" />

    <TextView
        android:id="@+id/test_tv_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:padding="20dp"
        android:text="HelloWorld" />

    <Button
        android:id="@+id/test_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove Background" />

</LinearLayout>


2.Activity 主要程式碼如下:

        testTv1 = (TextView) findViewById(R.id.test_tv_1);
        testTv2 = (TextView) findViewById(R.id.test_tv_2);
        testTv3 = (TextView) findViewById(R.id.test_tv_3);
        testBtn = (Button) findViewById(R.id.test_btn);

        testTv2.setBackgroundResource(R.drawable.test_drawable_without_padding);//不帶padding的drawable
        testTv3.setBackgroundResource(R.drawable.test_drawable_with_padding);//帶padding的drawable

        testBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testTv2.setBackgroundResource(0);
                testTv3.setBackgroundResource(0);
            }
        });


3.Activity啟動後的介面如下:



可以發現當backGround Drawable帶有Padding時,View原來的Padding被設定為該backGround Drawable的padding值了。即

If the background has padding, this View's padding is set to the background's padding.


4.“REMOVE BACKGROUND” button click



當remove帶padding的Drawable後,View的Padding值是不會改變的(即還是remove之前帶padding 的backDrawable的padding)。即

However, when a background is removed, this View's padding isn't
touched.



相關文章