View.setBackgroundResource(int resId)当resId代表的Drawable带有Padding时,会改变View本身的Padding值

来源:互联网 发布:大数据分析师工资待遇 编辑:程序博客网 时间:2024/05/18 13:41

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'ttouched.



0 0
原创粉丝点击