【问题标题】:Changing background colour of an Activity based on a theme/style根据主题/样式更改活动的背景颜色
【发布时间】:2011-10-05 06:28:06
【问题描述】:

我有一个 Activity,它在清单中设置了样式资源(使用 android:theme="@style/blah"。我希望在 Java 代码中动态更改此 Activity 的背景颜色。我该怎么做?

谢谢!

【问题讨论】:

    标签: android android-activity background themes styles


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:id="@+id/activityRootContainer>
    
    </FrameLayout>
    

    然后在你的代码中:

    findViewById(R.id.activityRootContainer).setBackgroundDrawable(getResources().getDrawable(R.drawable.backgroundImage))
    

    findViewById(R.id.activityRootContainer).setBackgroundColor(Color.RED)
    

    HTH?

    【讨论】:

    • 干杯,成功了。我打算尝试类似的方法,但使用 LinearLayout,但这看起来更整洁。
    猜你喜欢
    • 2023-04-05
    • 2012-04-29
    • 2013-09-03
    • 1970-01-01
    • 2012-02-16
    • 2013-12-18
    • 2020-03-27
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多