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
- View.setBackgroundResource(int resId)当resId代表的Drawable带有Padding时,会改变View本身的Padding值
- view Padding的理解
- getString(int resId, Object... formatArgs)的用法
- setBackgroundResource 9 patch图会影响 LinearLayout的Padding
- Resources类中getString (int ResID)与getText (int ResID)的区别
- 如何获取view.setBackgroundResource里参数的int值
- View inflate(resId, ViewGroup , boolean attachToRoot)和View inflate(int resource, ViewGrou)区别
- setBackgroundResource后Padding失效的bug
- setBackgroundResource后Padding失效的bug
- Context类getString(@StringRes int resId, Object... formatArgs)的使用
- View中Margin与Padding的区别
- 动态设置view的padding和margin值
- 动态设置view的padding和margin值
- Android自定义View去除TextView的Padding值
- Android 自定义View时处理wrap_content和padding的方法
- Android代码调用setBackgroundResource引起的Padding问题
- Android getText(int resId)和getString(int resId)
- Android getText(int resId)和getString(int resId)
- 4.mongoDB shell 之增删改操作
- MVC与三层架构的区别
- 掌握需求过程-读书记录
- webpack中的不足--Tree-shaking
- Floyd算法求图最短路径及实现
- View.setBackgroundResource(int resId)当resId代表的Drawable带有Padding时,会改变View本身的Padding值
- XML签名验签
- 矩阵快速幂(细胞自动机,LA 3704)
- Spring 定时任务--quartz
- Python建议:考虑兼容性,尽可能使用Unicode
- 巧妙设置 解决IIS6.0服务器访问难题
- iOS AFL代码布局
- IAP详解
- yarn