相关疑难解决方法(0)

123
推荐指数
6
解决办法
24万
查看次数

单击以编程方式更改布局 - 背景

我可以通过写入来改变LinearLayout的背景

机器人:背景="@绘制/概述"

到我的LinearLayout.

但是,我无法以编程方式执行此操作.我试过以下:

LinearLayout horizo​​ntal_menu = new LinearLayout(this); ... horizo​​ntal_menu.setBackgroundResource(getResources().getInteger(R.drawable.overview));

以及代码来源:

horizo​​ntal_menu.setBackground(getResources()getDrawable(R.drawable.overview));

第一次尝试在运行时抛出RuntimeException,第二行似乎什么都不做 - >没有错误,没有异常,没有更改背景点击...

- > overview.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
<item android:state_focused="true" android:drawable="@color/half_transparent"/> 
<item android:state_pressed="true" android:drawable="@color/half_transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)

我希望有人能帮助我!

谢谢!

编辑:

LinearLayout content = (LinearLayout)findViewById(R.id.LinearLayout); 
LinearLayout horizontal_menu = new LinearLayout(this); 
horizontal_menu.setOrientation(LinearLayout.HORIZONTAL); 
horizontal_menu.setBackgroundResource(getResources().getInteger(R.drawable.overv??iew)); 
content.addView(horizontal_menu);
Run Code Online (Sandbox Code Playgroud)

xml android response

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×2

android-activity ×1

colors ×1

java ×1

response ×1

xml ×1