动态改变布局背景颜色无效

莫为难 2016-03-16 02:34:17
布局名称为 bottom.xml 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/base_color_text_white"
android:orientation="horizontal" >

<LinearLayout
android:id="@+id/id_main_pager"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:onClick="onMainPager"
android:orientation="vertical" >

<ImageButton
android:id="@+id/id_main_pager_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:clickable="false"
android:contentDescription="@string/app_name"
android:src="@drawable/bottom_main_pager_an" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_pager" />
</LinearLayout>
.....
</LinearLayout>

我想在代码里动态改变布局的背景颜色,代码如下:
View in = LayoutInflater.from(this).inflate(R.layout.bottom, null);
in.setBackgroundColor(Color.BLUE);

但是颜色就是不变,有高手指点下吗?
...全文
358 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
莫为难 2016-03-18
  • 打赏
  • 举报
回复
引用 4 楼 u014165119 的回复:
View in = LayoutInflater.from(this).inflate(R.layout.bottom, null);返回的View(in)是一个新的对象,跟你setContentView()中layout引入的不是同一个对象。 你应该使用findViewByID的方式取出再修改背景。
确实是这样,谢谢啦。
StoneHui_ 2016-03-16
  • 打赏
  • 举报
回复
View in = LayoutInflater.from(this).inflate(R.layout.bottom, null);返回的View(in)是一个新的对象,跟你setContentView()中layout引入的不是同一个对象。 你应该使用findViewByID的方式取出再修改背景。
莫为难 2016-03-16
  • 打赏
  • 举报
回复
引用 1 楼 RunningShadow_007 的回复:
有两种原因 1 你的填充完的view (in)setContentView()中是否有用到 2 setBackgroundColor参数问题
View (in)是在setContentView里面layout用include引用的。 我单独的拿in这个View做setContentView里面的参数,照样还是不能改变颜色。
RunningShadow_007 2016-03-16
  • 打赏
  • 举报
回复
为什么不直接拿到linearlayout 然后给linearlayout设置背景颜色?
RunningShadow_007 2016-03-16
  • 打赏
  • 举报
回复
有两种原因 1 你的填充完的view (in)setContentView()中是否有用到 2 setBackgroundColor参数问题

80,354

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧