Android 自定义相对设置上的setBackground Yout不工作

Android 自定义相对设置上的setBackground Yout不工作,android,android-layout,Android,Android Layout,全部, 我有一个自定义的RelativeLayout,加载视图后,我想将RelativeLayout的背景更改为可绘制的。我不明白为什么它不工作…看起来应该这么简单 以下是我的自定义布局代码: public class InputBoxView extends RelativeLayout { //My variables //Irrelevants initalizers //Method in question public void addErrorB

全部,

我有一个自定义的
RelativeLayout
,加载视图后,我想将
RelativeLayout
的背景更改为可绘制的。我不明白为什么它不工作…看起来应该这么简单

以下是我的自定义布局代码:

public class InputBoxView extends RelativeLayout {

    //My variables

    //Irrelevants initalizers

    //Method in question
    public void addErrorBox() {
        setBackgroundResource(R.drawable.error_border);
    }

}
以下是自定义视图的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/standard_line_height"
    android:background="@android:color/white" >

    <View
        style="@style/space_view"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">

        <TextView
            android:id="@+id/input_box_textview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".35"
            android:padding="10dp"
            android:text="Key" />

        <EditText
            android:id="@+id/input_box_edittext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.65"
            android:layout_marginRight="10dp" />
    </LinearLayout>

    <View
        style="@style/space_view"
        android:layout_alignParentBottom="true" />

</RelativeLayout>
当我从活动中调用
addErrorBox
方法时,我的背景不会明显改变,但是如果我在
InputBoxView
的实例上设置断点,
mBackground
属性在我调用
addErrorBox
后会改变,因此我认为背景正在改变,但是没有更新

我尝试在
InputBoxView
的实例上调用
invalidate()
,但仍然不起作用


有人有想法吗?

根据我对您提供的代码进行的测试,问题在于

android:background="@android:color/white"
在自定义视图xml的相对布局上。它似乎忽略了使用
setBackgroundResource
InputBoxView
设置的内容

删除该属性后,我可以看到红色边框背景

解决此问题的一个方法是从xml中删除
RelativeLayout
,因为您的
InputBoxView
已经从它扩展,并在膨胀自定义视图时设置白色背景、宽度和高度等等

因此,您的xml可以如下所示

<merge xmlns:android="http://schemas.android.com/apk/res/android" >
   <!-- Your views in here without the RelativeLayout -->
</merge>
或者,您可以将ID添加到RelativeLayout并直接修改背景,而不是在
InputBoxView

对于,在onCreate之后需要它,以便重新绘制


希望这有助于解决您的问题,或者至少为您指明正确的方向。

这太疯狂了。这不应该是虫子吗?
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
   <!-- Your views in here without the RelativeLayout -->
</merge>
public InputBoxView(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.some_custom, this, true);

    // Set what you need for the relative layout.
    setBackgroundColor(Color.WHITE);

}

public void addErrorBox() {
    setBackgroundResource(R.drawable.error_border);
    invalidate();
}