切换风格

发布于 2025-01-06 13:07:05 字数 1315 浏览 3 评论 0原文

假设我有多个主题的 styles.xml 文件(当然名称不同)。是否可以选择应用程序应从中提取哪个文件?

白色样式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="mybutton">
        <item name="android:layout_width">45dp</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:textColor">#202020</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@drawable/white_btnbg</item>
    </style>

...

黑色样式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="mybutton">
        <item name="android:layout_width">45dp</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:textColor">#FFFFFF</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@drawable/black_btnbg</item>
    </style>

...

那么,在我的布局 xml 中,所有按钮的样式都将设置为“mybutton”,并根据用户选择的主题从协调文件中提取。 由于不可能在运行时更改视图的样式,是否可以这样做?或者有更好的方法(我确信有)来改变风格?

Let's say I have multiple styles.xml files (with different names of course) for themes. Is it possible to choose which file the app should pull from?

White style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="mybutton">
        <item name="android:layout_width">45dp</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:textColor">#202020</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@drawable/white_btnbg</item>
    </style>

...

Black style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="mybutton">
        <item name="android:layout_width">45dp</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:textColor">#FFFFFF</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@drawable/black_btnbg</item>
    </style>

...

So then, in my layout xml all my button's styles would be set to "mybutton" and depending which theme the user chose it would pull from the coordinating file.
Since it isn't possible to change a view's style at runtime is it possible to do this? Or is there a better way (i'm sure there is) to change styles?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红玫瑰 2025-01-13 13:07:05

使用具有不同名称的多种样式。然后您可以通过编程方式设置背景:

Button b = new Button(this);
b.setBackgroundResource(R.drawable.blabla);

Use multiple styles with different names. Then you can set the backGround programmatically:

Button b = new Button(this);
b.setBackgroundResource(R.drawable.blabla);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文