在Android中更改按钮的字体大小,同时保留原生状态

时间:2015-08-31 09:47:29

标签: android qml qt5 qtquickcontrols

我想更改按钮的字体大小。我使用了here

的解决方案
import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2

Rectangle {
  id: container
  width: 800
  height: 800

  Button {
    id: cmdQuit
    text: qsTr("Quit")
    width: 100
    height: 100
    style: ButtonStyle {
      label: Text {
        renderType: Text.NativeRendering
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
        font.pointSize: 20
        text: cmdQuit.text
      }
    }
  }
}

不幸的是,当我这样做时,我失去了Android原生外观,并获得了后备基础外观。有没有办法在不丢失Android上的原生样式的情况下更改字体大小?

我想改变单个按钮的外观,并希望其他按钮保持不变。我正在使用Qt5(C ++)和QML。我不想要一个涉及复制整个QtQuick / Controls / Styles / Android文件夹的解决方案 - 我可以自己做,但它很糟糕

3 个答案:

答案 0 :(得分:6)

警告:此解决方案依赖于修改内部对象,并且无法保证在不同的Qt版本或平台上工作

如果您绝对只想修改未公开的对象的属性,您仍然可以使用children的{​​{1}}属性(或Item / {{}来访问该属性1}})。

调用resources的一个方便的函数是转储data的层次结构的函数,这是一个示例实现:

Component.omCompleted

以及它在Android和Qt 5.5上为Item输出的内容:

function dumpHierarchy(item, level) {
    level = level | 0;
    for (var index = 0; index < item.children.length; ++index) {
        var child = item.children[index];
        print(Array(level*2 + 1).join(" ")+ index +": "+child);
        dumpHierarchy(child, level+1);
    }
}

在这里,我们可以看到我们感兴趣的Button的层次结构位置。 如果您想快速而又肮脏,可以在0: QQuickLoader(0xab87c768) 0: QQuickLoader_QML_44(0xab7a84f0) 1: QQuickItem_QML_53(0xab8c8d60) 0: DrawableLoader_QMLTYPE_51(0xab8bb088) 0: StateDrawable_QMLTYPE_65(0xab949420) 0: DrawableLoader_QMLTYPE_51(0xab936ea0) 0: NinePatchDrawable_QMLTYPE_67(0xab955c90) 0: QQuickAndroid9Patch_QML_68(0xab913608) 1: QQuickLoader_QML_66(0xab924838) 1: QQuickRowLayout(0xab8b03a0) 0: QQuickImage(0xab8b0960) 1: LabelStyle_QMLTYPE_50(0xab8c1758) 1: QQuickMouseArea_QML_46(0xab887028) 元素中添加:

LabelStyle

但这不是真正可维护的,更可接受的解决方案是遍历Button的子层次结构并找到要修改的字体。

我在函数应用程序中添加了函数Component.onCompleted: children[0].children[1].children[1].children[1].font.pixelSize = 50

Button

答案 1 :(得分:0)

您是否尝试过使用Usefont.pixelSize

或尝试以下

QFont f( "Arial", 10, QFont::Bold);
textLabel->setFont( f);

答案 2 :(得分:0)

您可以尝试从 TextView 继承Button.setTextSize() 按钮。如果您在设计时执行此操作,则可以使用textSize属性。 如果您在设计时执行此操作,则可以按如下方式应用textSize:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_gravity="center"
    android:textSize="25sp"
    />

当您从java代码执行此操作时,您可以执行以下操作:

// Set textsize to 20px here
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);