设置背景可绘制对象时,填充会去哪里?



我的EditTextButton视图上有这个问题,我有一个很好的填充物供它们远离文本,但是当我用setBackgroundDrawablesetBackgroundResource更改背景时,该填充将永远丢失。

我发现添加 9 补丁作为背景资源重置填充 - 尽管有趣的是,如果我添加颜色或非 9 补丁图像,它没有。 解决方案是在添加背景之前保存填充值,然后在之后再次设置它们。

private EditText value = (EditText) findViewById(R.id.value);
int pL = value.getPaddingLeft();
int pT = value.getPaddingTop();
int pR = value.getPaddingRight();
int pB = value.getPaddingBottom();
value.setBackgroundResource(R.drawable.bkg);
value.setPadding(pL, pT, pR, pB);

我能够将元素包装在另一个布局中,在这种情况下,一个FrameLayout .这使我能够在不破坏所包含RelativeLayout上的填充的情况下更改FrameLayout的背景。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/commentCell"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/comment_cell_bg_single" >
    <RelativeLayout android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="20dp" >
    <ImageView android:id="@+id/sourcePic"
               android:layout_height="75dp"
               android:layout_width="75dp"
               android:padding="5dp"
               android:background="@drawable/photoframe" 
     />
...

另一种选择是在设置背景Drawable后以编程方式设置它,如上所述。只需确保计算像素以校正设备的分辨率即可。

我在TextView

中遇到了这个问题,所以我对TextView进行了子类化,并制作了TextView.setBackgroundResource(int resid)方法的Override 方法。喜欢这个:

@Override
public void setBackgroundResource(int resid) {
    int pl = getPaddingLeft();
    int pt = getPaddingTop();
    int pr = getPaddingRight();
    int pb = getPaddingBottom();
    super.setBackgroundResource(resid);
    this.setPadding(pl, pt, pr, pb);
}

这样,它在设置资源之前获取项的填充,但实际上不会弄乱方法的原始功能,除了保留填充。

还没有对此进行超级彻底的测试,但此方法可能有用:

    /**
 * Sets the background for a view while preserving its current padding. If the background drawable
 * has its own padding, that padding will be added to the current padding.
 * 
 * @param view View to receive the new background.
 * @param backgroundDrawable Drawable to set as new background.
 */
public static void setBackgroundAndKeepPadding(View view, Drawable backgroundDrawable) {
    Rect drawablePadding = new Rect();
    backgroundDrawable.getPadding(drawablePadding);
    int top = view.getPaddingTop() + drawablePadding.top;
    int left = view.getPaddingLeft() + drawablePadding.left;
    int right = view.getPaddingRight() + drawablePadding.right;
    int bottom = view.getPaddingBottom() + drawablePadding.bottom;
    view.setBackgroundDrawable(backgroundDrawable);
    view.setPadding(left, top, right, bottom);
}

使用它代替 view.setBackgroundDrawable(Drawable)。

只是为了解释发生了什么:

这实际上是一个功能。可以用作背景的布局可绘制对象可以通过以下方式定义填充:

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingRight="8dp"
    >
    ...
</layer-list>

此填充将与新的可绘制背景一起设置。如果没有填充,默认值为 0。

更多信息来自Romain Guy撰写的电子邮件:https://www.mail-archive.com/android-developers@googlegroups.com/msg09595.html

向后兼容版本的 cottonBallPaws 的答案

/** 
  * Sets the background for a view while preserving its current     padding. If the background drawable 
  * has its own padding, that padding will be added to the current padding. 
 *  
 * @param view View to receive the new background. 
 * @param backgroundDrawable Drawable to set as new background. 
 */ 
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("deprecation")
public static void setBackgroundAndKeepPadding(View view, Drawable backgroundDrawable) {
    Rect drawablePadding = new Rect();
    backgroundDrawable.getPadding(drawablePadding);
    int top = view.getPaddingTop() + drawablePadding.top;
    int left = view.getPaddingLeft() + drawablePadding.left;
    int right = view.getPaddingRight() + drawablePadding.right;
    int bottom = view.getPaddingBottom() + drawablePadding.bottom;
    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        view.setBackgroundDrawable(backgroundDrawable);
    } else {
        view.setBackground(backgroundDrawable);
    }
    view.setPadding(left, top, right, bottom);
}

您可以通过使用 9 个修补程序的图像并在可绘制对象中定义内容区域来提供一些填充。检查这个

您还可以从 xml 或编程方式在布局中设置填充

XML 填充标记

android:padding
android:paddingLeft
android:paddingRight
android:paddingTop
android:paddingBottom

调用 setBackgroundDrawable 后,您可以尝试通过调用 setPadding 在编辑文本或按钮视图上手动设置填充

对于普通搜索者,

只需在 setBackgroudDrawable 之后添加 setPadding 即可。更改可绘制对象时,必须再次调用 setPadd。

喜欢:

view.setBackgroundDrawable(backgroundDrawable);
view.setPadding(x, x, x, x);

最干净的方法是在指向可绘制图像文件的 xml 可绘制对象中定义填充

大英斯

我的解决方案是扩展视图(在我的例子中是 EditText)并覆盖setBackgroundDrawable()setBackgroundResource()方法:

// Stores padding to avoid padding removed on background change issue
public void storePadding(){
    mPaddingLeft = getPaddingLeft();
    mPaddingBottom = getPaddingTop();
    mPaddingRight = getPaddingRight();
    mPaddingTop = getPaddingBottom();
}
// Restores padding to avoid padding removed on background change issue
private void restorePadding() {
    this.setPadding(mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom);
}
@Override
public void setBackgroundResource(@DrawableRes int resId) {
    storePadding();
    super.setBackgroundResource(resId);
    restorePadding();
}
@Override
public void setBackgroundDrawable(Drawable background) {
    storePadding();
    super.setBackgroundDrawable(background);
    restorePadding();
}

结合所有解决方案,我在 Kotlin 中写了一个:

fun View.setViewBackgroundWithoutResettingPadding(@DrawableRes backgroundResId: Int) {
    val paddingBottom = this.paddingBottom
    val paddingStart = ViewCompat.getPaddingStart(this)
    val paddingEnd = ViewCompat.getPaddingEnd(this)
    val paddingTop = this.paddingTop
    setBackgroundResource(backgroundResId)
    ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
}
fun View.setViewBackgroundWithoutResettingPadding(background: Drawable?) {
    val paddingBottom = this.paddingBottom
    val paddingStart = ViewCompat.getPaddingStart(this)
    val paddingEnd = ViewCompat.getPaddingEnd(this)
    val paddingTop = this.paddingTop
    ViewCompat.setBackground(this, background)
    ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
}

只需在 Android Studio 中将 lib 更改为 v7:22.1.0 即可,如下所示编译 'com.android.support:appcompat-v7:22.1.0'

大多数答案都是正确的,但应该正确处理背景设置。

首先获取视图的填充

//Here my view has the same padding in all directions so I need to get just 1 padding
int padding = myView.getPaddingTop();

然后设置背景

//If your are supporting lower OS versions make sure to verify the version
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    //getDrawable was deprecated so use ContextCompat                            
    myView.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.bg_accent_underlined_white));
} else {
    myView.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_accent_underlined_white));
}

然后设置视图在背景更改前的填充

myView.setPadding(padding, padding, padding, padding);

我找到了另一种解决方案。我在按钮上也遇到了类似的问题。最终,我补充说:

android:scaleX= "0.85"
android:scaleY= "0.85"

它对我有用。默认填充几乎相同。

就我而言,我有一个可绘制对象,而且非常愚蠢,我没有看到填充在 xml 中全部设置为 0。

也许它与某人有关

在选择器中使用可绘制对象的小技巧不会删除填充

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/anything" />
</selector>

这里是cottonBallPaws的setBackgroundAndKeepPadding的改进版本。即使您多次调用该方法,也会保持填充:

/**
 * Sets the background for a view while preserving its current padding. If the background drawable
 * has its own padding, that padding will be added to the current padding.
 */
public static void setBackgroundAndKeepPadding(View view, Drawable backgroundDrawable) {
    Rect drawablePadding = new Rect();
    backgroundDrawable.getPadding(drawablePadding);
    // Add background padding to view padding and subtract any previous background padding
    Rect prevBackgroundPadding = (Rect) view.getTag(R.id.prev_background_padding);
    int left = view.getPaddingLeft() + drawablePadding.left -
            (prevBackgroundPadding == null ? 0 : prevBackgroundPadding.left);
    int top = view.getPaddingTop() + drawablePadding.top -
            (prevBackgroundPadding == null ? 0 : prevBackgroundPadding.top);
    int right = view.getPaddingRight() + drawablePadding.right -
            (prevBackgroundPadding == null ? 0 : prevBackgroundPadding.right);
    int bottom = view.getPaddingBottom() + drawablePadding.bottom -
            (prevBackgroundPadding == null ? 0 : prevBackgroundPadding.bottom);
    view.setTag(R.id.prev_background_padding, drawablePadding);
    view.setBackgroundDrawable(backgroundDrawable);
    view.setPadding(left, top, right, bottom);
}

您需要通过值/.xml定义资源 ID:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="prev_background_padding" type="id"/>
</resources>

我使用这个非常简单的解决方法,我在style.xml中定义了一个accentColor,如下所示

<item name="colorAccent">#0288D1</item>

然后我在Button标签中使用以下任一样式

style="@style/Base.Widget.AppCompat.Button.Colored"
style="@style/Base.Widget.AppCompat.Button.Small"

例如:

<Button
    android:id="@+id/btnLink"
    style="@style/Base.Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tvDescription"
    android:textColor="@color/textColorPrimary"
    android:text="Visit Website" />
<Button
    android:id="@+id/btnSave"
    style="@style/Base.Widget.AppCompat.Button.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tvDescription"
    android:layout_toRightOf="@id/btnLink"
    android:textColor="@color/textColorPrimaryInverse"
    android:text="Save" />

最新更新