app字体不随系统改变而变化

在activity的基类里面重写以下方法:(自留用)

//系统设置发生改变的时候会触发以下方法:(如屏幕方向,键盘和文字大小等)
@Override
public void onConfigurationChanged(Configuration newConfig) {
    //不是默认值
    if (newConfig.fontScale != 1)
        getResources();
    super.onConfigurationChanged(newConfig);
}
//重写 getResource() 方法,修改 configuration 为 setToDefaults()
@Override
public Resources getResources() {
    Resources res = super.getResources();
    //非默认值
    if (res.getConfiguration().fontScale != 1) {
        Configuration newConfig = new Configuration();
        //设置默认
        newConfig.setToDefaults();
        res.updateConfiguration(newConfig, res.getDisplayMetrics());
    }
    return res;
}

也可以在application的oncreate中设置:

// 加载系统默认设置,字体不随用户设置变化
 Resources res = super.getResources();
 Configuration config = new Configuration();
 config.setToDefaults();
 res.updateConfiguration(config, res.getDisplayMetrics());

也可以在application中重写getResources;

还有一个是网上提供的方法,将字体设置为dp,没有试过,有意向的可以尝试一下。

感谢下面的博主:

https://blog.csdn.net/qq_31492865/article/details/82453968

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 PyQt6 中,如果你想要在 QPushButton 的点击事件中更改字体颜色,你可以这样做: 首先,你需要设置按钮的连接信号和槽(slot)。当按钮被点击时,会触发自定义的槽函数,这个函数可以修改按钮的文字颜色。 ```python from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QLabel from PyQt6.QtGui import QFont, QColor, QTextCursor class MyWindow(QWidget): def __init__(self): super().__init__() self.button = QPushButton("点击我") self.label = QLabel("默认文字颜色") self.button.clicked.connect(self.change_font_color) layout = QVBoxLayout() layout.addWidget(self.button) layout.addWidget(self.label) self.setLayout(layout) def change_font_color(self): current_color = self.label.textColor() # 获取当前颜色 new_color = QColor(current_color.name()) # 创建新的颜色,这里假设新颜色是反色 new_color.setHsv(current_color.hue(), 255, 255) # 修改HSV模式的颜色值,这里是变亮 self.label.setTextColor(new_color) # 设置新的字体颜色 cursor = self.label.textCursor() # 获取光标位置 cursor.movePosition(QTextCursor.End) # 移动到文字结束处 self.label.setTextCursor(cursor) # 更新光标位置 if __name__ == "__main__": app = QApplication([]) window = MyWindow() window.show() app.exec_() ``` 在这个例子中,每次点击按钮,按钮的文字颜色都会变成它的互补色,并保持高对比度。如果需要更复杂的颜色变化逻辑,你可以根据需求调整`new_color`部分的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值