【问题标题】:TextView font size auto scalingTextView 字体大小自动缩放
【发布时间】:2012-09-10 09:53:32
【问题描述】:

我有布局,它包含两部分:标题和正文。标题包含 TextView 和图像。标题高度可以通过编程方式更改。并且标题的图像和文本必须分别缩放。图像缩放可以,但文本不想缩放。在 Android 上是否可行?如何做到这一点?

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="UseCompoundDrawables" >

        <TextView
            android:id="@+id/title_text"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/app_name" />

        <ImageView
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/edit"
            android:src="@drawable/widget_edit" />
    </LinearLayout>

    <TextView
        android:id="@+id/content_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

【问题讨论】:

标签: android xml layout scale


【解决方案1】:

我认为这对于默认的 TextView 是不可行的 - 至少在没有一些黑客攻击的情况下是不可行的。

我会尝试为此使用自定义 View 类,并在其 Canvas 上绘制文本。在绘制调用期间,您知道画布的确切宽度和高度,并使用适当的字体大小绘制文本。

【讨论】:

    【解决方案2】:

    如果您以编程方式更改高度,您可以定义一个公式并计算文本的大小。我认为这是一种独特的方式。

    您可以实现自定义视图并在父级高度的函数中定义大小。

    存在一种单位,可以是util、sp(Scale-independent Pixels)。但这与系统中设置的文本大小有关。

    【讨论】:

    • 感谢大家的解答!我会考虑的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2011-06-16
    • 2019-04-06
    • 1970-01-01
    • 2014-04-18
    • 2013-03-16
    • 1970-01-01
    相关资源
    最近更新 更多