简体   繁体   English

更改CSS主题

[英]Changing CSS Themes

I've made a nice little application using java and javafx and have recently started to style it with CSS. 我已经使用java和javafx开发了一个不错的小应用程序,并且最近开始使用CSS对其进行样式设置。 I thought of the idea to have separate themes (one yellow, one blue, etc.) but then realized I know of no good way to do this. 我想到这个想法有不同的主题(一个黄色,一个蓝色等),但是后来意识到我知道这样做没有什么好方法。 In java if I wanted something to change I'd simply change the value of a variable but in CSS I don't think that's possible. 在Java中,如果我想更改某些内容,我只会更改变量的值,但是在CSS中,我认为这是不可能的。 So to my question, what is the best way to change themes by the click of a button (inside the application)? 因此,对于我的问题,通过单击按钮(在应用程序内部)更改主题的最佳方法是什么? Is it as I have feared, do I have to make a separate style sheet for every theme and switch between style sheets? 是否如我所担心的那样,是否必须为每个主题制作一个单独的样式表并在样式表之间切换? Thank you for any help. 感谢您的任何帮助。

The correct approach in JavaFX-CSS is to use color variables (and maybe the derive/ladder functions). JavaFX-CSS中正确的方法是使用颜色变量(以及派生/梯形图函数)。 So you most likely have 3 CSS-Files: 因此,您很可能拥有3个CSS文件:

  • style.css - with the selectors for your components and references to color variables style.css-具有组件的选择器和对颜色变量的引用
  • theme-default.css - with the default color variables theme-default.css-具有默认颜色变量
  • theme-blue.css - with your blue color variables theme-blue.css-具有蓝色变量

Your style.css could look like this: 您的style.css可能如下所示:

.button {
  -fx-background-color: my-button-background-color;
}

And your theme-default.css would be 而您的theme-default.css将是

.root {
  my-button-background-color: #f00;
}

And your theme-blue.css would be: 您的theme-blue.css将是:

.root {
  my-button-background-color: #00f;
}

If you want to flip themes you'd simple remove the theme-default.css from the Scene and add the theme-blue.css (switching back you'd do the opposite) in pseudo code: 如果您想翻转主题,只需简单地从场景中删除theme-default.css并添加伪代码中的theme-blue.css(切换回去,您将做相反的事情):

Scene s = ...
s.getStylesheet().addAll( "style.css", "theme-default.css" );

// ...
button.setOnAction( e -> {
   if( s.getStylesheet().contains( "theme-default.css" ) ) {
     s.getStylesheet().remove("theme-default.css");
     s.getStylesheet().add("theme-blue.css");
   } else {
     s.getStylesheet().remove("theme-blue.css");
     s.getStylesheet().add("theme-default.css");
   }
} );

Isn't the great power about CSS style sheets that you can just use them interchangeably? 可以互换使用CSS样式表的强大功能不是吗? Using seperate themes just by replacing the CSS file? 仅通过替换CSS文件即可使用单独的主题?

Anyhow, if you'd like to change the styles of your components at runtime, as I assume from: 无论如何,如果您想在运行时更改组件的样式,我假设:

Is it as I have feared, do I have to make a separate style sheet for every theme and switch between style sheets? 是否如我所担心的那样,是否必须为每个主题制作一个单独的样式表并在样式表之间切换?

You can re-style every component at runtime. 您可以在运行时重新设置每个组件的样式。 Every object in JavaFX extends from Node . JavaFX每个对象都从Node扩展。 Which in turn implements Styleable . 进而实现Styleable Which gives you multiple styling options. 这为您提供了多种样式选择。

In case you DO want to change entire stylesheets, you can add and remove style sheets on your scene as well. 如果确实要更改整个样式表,则也可以在场景中添加和删除样式表。

I know two ways to do what you need: 我知道两种方法可以满足您的需求:

1.- Change css link from a default theme default.css to a color theme blue.css . 1.-将CSS链接从默认主题default.css更改为颜色主题blue.css 2.- Change a class that modify the look and feel from the main container. 2.-更改一个可修改主容器外观的类。

The 1st way: Change css link 第一种方式:更改CSS链接

You only have to create two css files that will change only in the color. 您只需创建两个仅会改变颜色的css文件。

Live example: http://seantheme.com/color-admin-v1.9/admin/html/index_v2.html 实时示例: http : //seantheme.com/color-admin-v1.9/admin/html/index_v2.html

Click the settings icon (cog) to view the Color theme controller. 单击设置图标(齿轮)以查看“颜色”主题控制器。

The 2nd way: Add a class to the main container 第二种方式:将类添加到主容器

Let's say that you have 3 color themes for a website, you have use one class .red or .red-theme that will be added to the main container as it changes every selector color underneath. 假设您有一个网站的3个颜色主题,您使用了一个.red.red-theme ,当它更改下面的每种选择器颜色时,它们都会添加到主容器中。

EX: I made a live test for a clearer answer, but I will use a contextual state for a box with information that will change depending on it's status, so we will have .warning , .info and .success state and regular theme which class is .post-wrapper . 例:我进行了现场测试以获得更清晰的答案,但我将为包装盒使用上下文状态,信息会根据其状态而变化,因此我们将具有.warning.info.success状态以及常规主题,该类是.post-wrapper

/* General Classes
* First we will give theme bases that include padding, margin, font-size 
* or default colors, etc.
*/
.post-wrapper{ /* Regular theme */
  padding: 15px;
  margin-bottom: 30px;
  border: 1px solid #ccc;
  box-shadow: 2px 2px 2px rgba(0,0,0, 0.2);
  border-radius: 5px;
}

And our HTML will look like: 我们的HTML将如下所示:

<article class="post-wrapper">
  ...
</article>

Then we set our color themes: 然后我们设置颜色主题:

/* CSS THEMES 
* Here we will set every color that will change with our current state.
* It could change colors, widths, font-sizes, :hover, or anything that we need
* to be changed.
*/
.warning{
  color: #9A2C2C;
  background-color: #FFE2E2;
  border-color: #9A2C2C;
}

.info{
  color: #2D5E7D;
  background-color: #CEEAF1;
  border-color: #2D5E7D; 
}

.success{
  color: #2D7D36;
  background-color: #CEF1D2;
  border-color: #2D7D36;
}

And our HTML will change depending to the state: 而且我们的HTML会根据状态而改变:

<article class="post-wrapper info">
  ...
</article>

<article class="post-wrapper success">
  ...
</article>

<article class="post-wrapper warning">
  ...
</article>

Live example: https://jsfiddle.net/xwazzo/Ls9f313g/2/ 实时示例: https //jsfiddle.net/xwazzo/Ls9f313g/2/

I will leave here a bootstrap example that shows Contextual Colors: http://getbootstrap.com/css/#helper-classes-colors 我将在此处留下一个显示上下文颜色的引导程序示例: http : //getbootstrap.com/css/#helper-classes-colors

Conclusions: 结论:

I like the 2nd option and have use it more because I haven't found a way to create multiple css files with different colors in an easy and fast way and website weight doesn't increase too much. 我喜欢第二个选项,并使用了更多选项,因为我还没有找到一种以简单快捷的方式创建具有不同颜色的多个CSS文件的方法,并且网站的重量不会增加太多。

I suppose that the 1st way is the best because you will have fewer classes on your style sheet and will save few KB. 我想第一种方法是最好的,因为样式表上的类较少,并且可以节省几KB。

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

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