繁体   English   中英

Android:根据布局文件更改可绘制矩形和 ActionBar 的颜色

[英]Android : Change color of drawable rectangle and ActionBar depending on layout file

我在一些听起来很基本的事情上被困了一段时间。 我想根据设备的方向使用两个布局文件更改 ActionBar 和两个可绘制矩形 colors。 我也有两个活动。

现在,我已将其编码为 controller(在 OnResume 中调用 function):

public void changeColor(int orientation) {
    String className = context.getClass().getSimpleName();

    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        if (className.equals("MainActivity")) {
            // Change actionbar, big square and small square colors
        }
        if (className.equals("WelcomeActivity")) {
            // Change actionbar color
        }
    } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        if (className.equals("MainActivity")) {
            // change actionbar, big square and small square colors
        } else if (className.equals("WelcomeActivity")) {
            // change actionbar color
        }
    }
}

这听起来像是可以使用纵向布局和横向布局来修复它们的 colors 不同的东西。 这可能吗? 现在有了这个 function,听起来我在重新发明轮子。

编辑:我的可绘制形状(矩形):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="25dp" />
    <solid android:color="#17D9C5" />
</shape>

您可以根据设备的方向提供不同的资源。 在您的情况下,添加一个颜色资源文件res/values/colors.xml用于纵向以及一个文件res/values-land/colors.xml专门用于横向。

然后,像这样在两个文件中定义 colors

<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorRectangle1">#00574B</color>
    <color name="colorRectangle2">#D81B60</color>
</resources>

并根据需要使用不同的 rgb 值。

接下来,使用Activity主题中的颜色资源。 您可以在Manifest.xml中为Activity设置主题,并在res/values/styles.xml中声明它。 ActionBar 的颜色由

<item name="colorPrimary">@color/colorPrimary</item>

最后同样重要的是,使用可绘制资源文件中的颜色资源:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="25dp" />
    <solid android:color="@color/colorRectangle1" />
</shape>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM