【问题标题】:Is it possible to dynamically set Android button text size in XML?是否可以在 XML 中动态设置 Android 按钮文本大小?
【发布时间】:2013-09-30 18:39:40
【问题描述】:

我正在开发一个应用程序,我在主菜单上有一系列按钮,我希望它们都具有相同的文本大小。我可以在属性文件中指定吗?

这是我当前的 XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="horizontal"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <Button
                android:id="@+id/sensors_available"
                android:text="@string/sensors_available"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </TableRow>
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <Button
                android:id="@+id/b_start_hm"
                android:text="@string/hm_start_service"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <Button
                android:id="@+id/b_stop_hm_service"
                android:text="@string/hm_stop_service"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </TableRow>
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <Button
                android:id="@+id/b_start_hot_video"
                android:text="@string/video_service_start"
                android:onClick="startHotVideoService"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <Button
                android:id="@+id/b_hv_stop_service"
                android:text="@string/hv_stop_service"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </TableRow>
</TableLayout>

我是否可以将所有12sp 替换为@string\custom_text_size 之类的东西,然后在string.xml 文件中定义它?当我尝试这个时,我在启动我的应用程序时遇到了一个致命的异常。

【问题讨论】:

标签: android xml


【解决方案1】:

你是如此接近,但你需要定义一个dimen而不是string

<dimen name="textSize12">12sp</dimen>

你的Button 看起来像这样。-

<Button
    android:id="@+id/b_stop_hm_service"
    android:text="@string/hm_stop_service"
    android:textSize="@dimen/textSize12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

理想情况下,dimen 资源定义在单独的资源文件中,例如 dimens.xml

【讨论】:

    【解决方案2】:

    在 res/values/styles.xml 中创建样式:

    <style name="TextSize">
        <item name="android:textSize">12sp</item>
    </style>
    

    在您的 XML 中声明:

    <Button
        android:id="@+id/b_start_hot_video"
        android:text="@string/video_service_start"
        android:onClick="startHotVideoService"
        style="@style/TextSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    通过使用样式,您可以存储多种设置,例如背景可绘制对象、textColor 等...

    【讨论】:

    • PS:恕我直言,没有必要为设置textSize创建样式。除非您设置更多属性(例如颜色、阴影、边距...),否则仅使用 @dimen 资源对我来说更有意义。
    • 同意@ssantos,我只是想给出一个更通用的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多