android-自定义RelativeLayout上的setBackground无法正常工作

android-自定义RelativeLayout上的setBackground无法正常工作,第1张

概述所有,我有一个自定义的RelativeLayout,在加载视图之后,我想将RelativeLayout的背景更改为可绘制的.我不知道为什么它不起作用…似乎应该很简单.这是我的自定义布局代码:publicclassInputBoxViewextendsRelativeLayout{//Myvariables//Irrelevantsinitalizers

所有,

我有一个自定义的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                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                androID:layout_alignParentBottom="true" /></relativeLayout>

这是我的error_border

<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:shape="rectangle" >    <solID androID:color="@androID:color/white" />    <stroke androID:wIDth="2dp" androID:color="@color/red"/></shape>

当我从活动中调用addErrorBox方法时,背景没有明显变化,但是如果我在inputBoxVIEw实例上放置一个断点,则在调用addErrorBox之后mBackground属性会发生变化,因此我相信背景会发生变化,但是没有更新.

我尝试在inputBoxVIEw实例上调用invalIDate(),但仍然无法正常工作.

有人有主意吗?

解决方法:

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

androID:background="@androID:color/white"

自定义视图xml的相对布局上.似乎忽略了您为inputBoxVIEw使用setBackgroundResource设置的任何内容.

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

一种解决方案是从xml中删除relativeLayout,因为inputBoxVIEw已经从它扩展了,并设置了白色背景,宽度高度以及在您为自定义视图充气时不设置的内容.

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

<merge xmlns:androID="http://schemas.androID.com/apk/res/androID" >   <!-- Your vIEws in here without the relativeLayout --></merge>

并在您的inputBoxVIEw中

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

或者,您可以将ID添加到relativeLayout并直接修改背景,而不是在inputBoxVIEw上.

对于invalidate,您需要在onCreate之后使用它,以便重新绘制它.

希望能帮助您解决问题或至少为您指明正确的方向.

总结

以上是内存溢出为你收集整理的android-自定义RelativeLayout上的setBackground无法正常工作全部内容,希望文章能够帮你解决android-自定义RelativeLayout上的setBackground无法正常工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1076678.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存