java - JavaFX/CSS 在组合框列表单元格的右侧显示 svg 形状
问题描述
我正在寻找创建一个组合框,所选项目应将其文本设置为粗体并在列表单元格的右侧显示一个勾号(勾号图标是 SVG):
目前在我的代码中,勾号占用了所有可用空间:
这是我的CSS:
.combo-box {
-fx-background-color: transparent;
-fx-border-color: #44494F
-fx-border-width: 1px
}
.combo-box .arrow {
-fx-background-color: white;
-fx-shape: -arrow;
}
.combo-box:showing .arrow {
-fx-rotate: 180;
}
.combo-box .list-cell {
-fx-font-size: 10px;
-fx-text-fill: white;
}
.combo-box-popup .list-view {
-fx-background-color: #2F353D;
}
.combo-box-popup .list-view .list-cell {
-fx-background-color: #2F353D;
-fx-border-width: 0px
}
.combo-box-popup .list-view .list-cell:filled:selected, .combo-box-popup .list-view .list-cell:filled:selected:hover{
-fx-font-weight: bold;
-fx-shape: "m184.405 461.013-184.405-184.405 24.354-24.354 160.051 160.051 332.279-332.279 24.354 24.354z";
}
.combo-box-popup .list-view .list-cell:filled:hover{
-fx-background-color: #393E44;
}
有没有办法只使用 CSS 将勾选放在右侧?即使没有,如何实现?
解决方案
正如@jewelsea 在评论中建议的那样,您可以提供一个自定义CellFactory
来处理大小:
ComboBox<String> cb = new ComboBox<>();
cb.setCellFactory(lv -> {
final ListCell<String> cell = new ListCell<>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(item != null ? item : null);
}
};
Region icon = new Region();
icon.getStyleClass().add("icon");
cell.setGraphic(icon);
cell.setGraphicTextGap(20);
return cell;
});
您可以在以下位置设置大小和样式css
:
.combo-box-popup .list-view .list-cell .icon {
-fx-min-width: 10;
-fx-pref-width: 10;
-fx-max-width: 10;
-fx-min-height: 10;
-fx-pref-height: 10;
-fx-max-height: 10;
-fx-shape: "m184.405 461.013-184.405-184.405 24.354-24.354 160.051 160.051 332.279-332.279 24.354 24.354z";
-fx-background-color: white;
}
.combo-box-popup .list-view .list-cell {
-fx-content-display: text-only;
}
.combo-box-popup .list-view .list-cell:filled:selected {
-fx-content-display: right;
}
输出:
推荐阅读
- python - 如何发出无限的 Rest API 请求并存储信息?
- java - 如何在 Eclipse 或 Netbeans 上安装 Selenium selenium-java-3.141.59 库?
- arrays - 错误:对象作为 React 子对象无效,但我的数据是对象数组?
- jmeter - 从 JMeter 登录 XMPP OpenFire 服务器时出现 SmackException NoResponseException
- c - 如何处理-?在命令行中使用 C?
- azure-devops-migration-tools - 迁移尝试失败。TF26176 错误:无法连接到指定的 Azure DevOps 服务器集合 URL
- java - 如果语句无法识别用户输入
- freertos - FreeRTOS:两个带中断的任务
- mysql - 用于过滤字符串化 json 数组的 SQL 查询
- javascript - 合并排序与 JS 中的用户输入比较