java在CSS中是否有使用区域宽度和高度百分比值的变通方法?
如果我们可以在CSS中使用百分比值表示区域的宽度和高度,这将非常方便,但是documentation声明:
Percentage values are not useful since the actual value would be computed from the width and/or height of the Regions's parent before the parent is laid out.
那么,这有解决办法吗?在将父对象加载到场景后,我尝试使用^{
Main。java
public class Main extends Application {
public static void main(String... args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("main.fxml")));
scene.getStylesheets().setAll(getClass().getResource("styles.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
main。fxml
<StackPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="Controller">
<Region fx:id="child"
styleClass="child"/>
</StackPane>
风格。css
.root {
-fx-pref-width: 500px;
-fx-pref-height: 500px;
}
.child {
-fx-background-color: teal;
-fx-pref-width: 50%;
-fx-pref-height: 50%;
}
控制器。java
public class Controller implements Initializable {
@FXML
private Region child;
@Override
public void initialize(URL location, ResourceBundle resources) {
Platform.runLater(() -> if (child != null) child.requestLayout());
}
}
共 (0) 个答案