【问题标题】:one control with different background color in different styles in android在android中具有不同样式的不同背景颜色的一个控件
【发布时间】:2015-04-29 15:45:43
【问题描述】:

我有 2 个活动: Activity1 , Activity2

Activity1的主题是蓝色,另一个是橙色

现在我有一个在这些活动中膨胀的 xml 布局 (sort_items.xml)

我想在我打开 Activity1 时将此布局的背景颜色设为蓝色,并在我打开 Activity2 时设为橙色。

我在styles.xml中创建了2个样式:

<style name="AppThemeOrange"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">

    <item name="actionBarStyle">@style/MyActionBarOrange</item>
</style>

<style name="AppThemeBlue"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">

    <item name="actionBarStyle">@style/MyActionBarBlue</item>
</style>

这些主题归因于它们在 manifest.xml 中的活动

colors.xml 中也有两种颜色

<color name="orange">#ffa726</color>
<color name="blue">#119eff</color>

这是 sort_items.xml

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="???"
    android:id="@+id/layout1">

    ...

 </LinearLayout>

现在如何设置在每个活动中自动更改的 layout1(在 sort_items.xml 中)背景颜色?

谢谢。

【问题讨论】:

    标签: android styles


    【解决方案1】:

    我不确定我是否理解,但你尝试过吗

    mLinearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.orange));
    

    【讨论】:

    • 谢谢,但是当我们知道打开了什么活动时,这段代码很有用。但是这个布局会在其他活动中膨胀,我们不知道那个活动是什么主题。
    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 2018-12-27
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2019-06-13
    • 1970-01-01
    相关资源
    最近更新 更多