91

我的视图EditTextButton视图中有这个问题,我有一个很好的填充物让它们远离文本,但是当我改变背景setBackgroundDrawablesetBackgroundResource填充物永远丢失时。

4

17 回答 17

102

我发现添加一个 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);
于 2013-02-25T05:58:33.380 回答
14

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

<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另一个选项是在按照上面的建议设置背景后以编程方式设置它。只需确保计算像素以校正设备的分辨率。

于 2012-07-03T19:16:31.687 回答
12

我在 TextView 中遇到了这个问题,所以我将 TextView 子类化并创建了该方法的 OverrideTextView.setBackgroundResource(int resid)方法。像这样:

@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);
}

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

于 2014-01-01T22:14:21.093 回答
9

还没有彻底测试过这个超级彻底,但这种方法可能有用:

    /**
 * 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)。

于 2013-01-24T00:08:04.583 回答
2

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

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

<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

于 2019-10-17T14:20:01.113 回答
2

向后兼容版本的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);
}
于 2015-09-16T22:07:40.260 回答
1

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

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

xml 填充标签

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

在调用 setBackgroundDrawable 后,您可以尝试在代码中手动设置填充,方法是在 EditText 或按钮视图上调用 setPadding

于 2012-04-10T19:39:24.437 回答
1

对于普通搜索者,

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

喜欢:

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

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

伟大的

于 2015-05-26T11:39:05.567 回答
1

结合所有的解决方案,我在 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)
}
于 2018-02-07T09:50:21.120 回答
1

我的解决方案是扩展视图(在我的情况下为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();
}
于 2017-03-09T18:15:47.483 回答
0

只需在 android studio 中将 lib 更改为 v7:22.1.0 就像这样 compile 'com.android.support:appcompat-v7:22.1.0'

于 2015-06-29T09:12:36.120 回答
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);
于 2016-06-16T17:01:20.300 回答
0

我找到了另一个解决方案。我遇到了与Buttons类似的问题。最后,我补充说:

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

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

于 2017-11-25T03:48:47.043 回答
0

也许它与某人有关

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/anything" />
</selector>
于 2022-02-27T23:04:08.397 回答
0

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

于 2019-11-25T14:42:39.527 回答
-1

这里是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);
}

您需要通过 values/ids.xml 定义资源 id:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="prev_background_padding" type="id"/>
</resources>
于 2014-04-08T14:42:50.810 回答
-1

我使用这个非常简单的解决方法,我accentColorstyle.xml下面定义了一个

<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" />
于 2015-09-30T17:03:10.513 回答