【问题标题】:RelativeLayout setBackgroundDrawable is not working [closed]相对布局 setBackgroundDrawable 不起作用[关闭]
【发布时间】:2014-11-14 11:16:08
【问题描述】:

我的问题:RelativeLayout setBackgroundDrawable 不起作用

没有错误,只有不改变或设置

我的代码

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_img1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.noiseframe.MainActivity" >

MainActivity.java

成员变量

Resources img1R;
Resources img2R;
Resources img3R;
Resources img4R;
Resources img5R;

Drawable img1D;
Drawable img2D;
Drawable img3D;
Drawable img4D;
Drawable img5D;

onCreate

relative = (RelativeLayout)inflater.inflate(R.layout.activity_main, null);

img1R = getResources();
img2R = getResources();
img3R = getResources();
img4R = getResources();
img5R = getResources();

img2D = img2R.getDrawable(R.drawable.main_img2);

relative.setBackgroundDrawable(img2D);

最后,处理程序将改变背景
但是..在任何地方写作,我的主要活动背景都没有改变..


谢谢你的好意






回到第一个代码 现在代码

    RelativeLayout relativelayout;
    relativelayout = new RelativeLayout(this);  
    relativelayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_img2));



没有错误!没有充气! 但是后面的图片是不会变的 TT...

【问题讨论】:

  • 试试 relative.setBackgroundResource(R.drawable.main_img2); - 而不是你写的所有内容。
  • MainActivity.setBackgroundDrawable(getResources().getDrawable(R.id.main_img2));是 main_img2 上的红线
  • and .. view using -> main_img2 上的红线
  • 所以你正在膨胀一个新的布局并在其中设置一个背景。稍后你对relative 布局做了什么,例如,你还是要把它放在屏幕上吗?
  • 帖子已修改。那是原始代码。我不认为这个错误没有改变@laalto

标签: java android xml android-layout


【解决方案1】:

不需要使用那么多变量。可以这样直接给背景图。

relative.setBackgroundDrawable(getResources().getDrawable(R.id.image_name));

如果您在 MainActivity 中并且您已经为您的 xml 文件使用了 setContentView,我还有一个问题。你为什么要在那里夸大xml文件?

【讨论】:

  • 找错部分,增加细节。第一个短。我会试试你的代码
  • 如果不充气,getDrawable(R.id.resource not found) 在main_img2上编码getDrawable(R.id.main_img2)红线时
  • 尝试.. MainActivity.setBackgroundDrawable(getResources().getDrawable(R.id.main_img2));但 main_img2 是红线
  • 找不到资源@Sid
  • 我能看到你的完整代码吗?如果可能的话,请将您的代码通过电子邮件发送给 mahesh.venkat27@gmail.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 2014-01-20
  • 2018-05-12
  • 2014-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多