简体   繁体   English

在TextView中更改textSize

[英]Change textSize in a TextView

In this code below from an android application on android studio, I have a LinearLayout that contains a TextView whose size I want to change for example say from 100 to 20. But the thing is, whenever I change the android:textSize="100.0dip" to any other number, the size still remains the same in the application when running it on my phone. 在以下来自android studio上的android应用程序的代码中,我有一个LinearLayout,其中包含一个TextView,我想将其大小更改为例如从100更改为20。但是问题是,每当我更改android:textSize =“ 100.0dip ”至其他任何数字,在手机上运行时,应用程序中的大小仍保持不变。

Code: 码:

<ScrollView
    android:id="@id/qp_body_wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/widget34"
    android:layout_below="@id/widget99"
    android:layout_marginBottom="5.0dip"
    android:layout_marginTop="10.0dip"
    android:clickable="true"
    android:focusable="true"
    android:foregroundGravity="right">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/qp_body_wrapper_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">

        <TextView
            android:id="@id/qp_body"
            style="@style/QuoteViewText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/dashboard_qod_bg"
            android:clickable="true"
            android:focusable="true"
            android:gravity="right"
            android:padding="10.0dip"
            android:textAlignment="viewEnd"
            android:textColor="#FFFFFF"
            android:textSize="100.0dip"
            tools:ignore="RtlCompat,RtlHardcoded" />
    </LinearLayout>
</ScrollView>

and here is the QuoteViewText style : 这是QuoteViewText样式:

<style name="QuoteViewText">
        <item name="android:textSize">100.0dip</item>
        <item name="android:typeface">serif</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#fffcfcfc</item>
        <item name="android:shadowColor">#ff102e46</item>
        <item name="android:shadowDx">0.0</item>
        <item name="android:shadowDy">1.0</item>
        <item name="android:shadowRadius">2.0</item>
    </style>

You are setting textview size 2 times one from applying style and another from applying textsize for the textview. 您正在将textview大小设置为应用样式的两倍,而另一次是为textview设置文本大小的两倍。 Use any one. 使用任何一个。 And for texts use sp instead of dp to honour user preferences. 对于文本,请使用sp而不是dp来满足用户的偏好。

Text size is measured in sp - Scale-independent Pixels. 文本大小以sp与比例无关的像素为单位。

So, in your case you should use 因此,您应该使用

<item name="android:textSize">100sp</item>

instead 代替

<item name="android:textSize">100.0dip</item>

Also, don't forget that style is overrided by properties from your layout, in this case you should remove android:textSize from your TextView and leave it just in styles. 另外,不要忘记样式会被布局中的属性所覆盖,在这种情况下,您应该从TextView删除android:textSize并将其保留为样式。 Also you should start from a text size of 10, 100 is to big and never used. 另外,您应该从10的文本大小开始,到100则要大并且永远不要使用。

You are using dip , instead you should sp for textSize . 您正在使用 ,而应该用上TEXTSIZE。

you can update your code files with these below files , it will solve your problem 您可以使用以下文件更新代码文件,这将解决您的问题

<ScrollView
    android:id="@id/qp_body_wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/widget34"
    android:layout_below="@id/widget99"
    android:layout_marginBottom="5.0dip"
    android:layout_marginTop="10dp"
    android:clickable="true"
    android:focusable="true"
    android:foregroundGravity="right">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/qp_body_wrapper_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">

        <TextView
            android:id="@id/qp_body"
            style="@style/QuoteViewText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/dashboard_qod_bg"
            android:clickable="true"
            android:focusable="true"
            android:gravity="right"
            android:padding="10dpp"
            android:textAlignment="viewEnd"
            android:textColor="#FFFFFF"
            tools:ignore="RtlCompat,RtlHardcoded" />
    </LinearLayout>
</ScrollView>

and here is the QuoteViewText style : 这是QuoteViewText样式:

 <style name="QuoteViewText">
            <item name="android:textSize">100sp</item>
            <item name="android:typeface">serif</item>
            <item name="android:textStyle">bold</item>
            <item name="android:textColor">#fffcfcfc</item>
            <item name="android:shadowColor">#ff102e46</item>
            <item name="android:shadowDx">0.0</item>
            <item name="android:shadowDy">1.0</item>
            <item name="android:shadowRadius">2.0</item>
        </style>

Tip : Use dimens.xml to store your concrete values for easier and cleaner code management. 提示 :使用dimens.xml存储您的具体值,以便更轻松,更干净地进行代码管理。

Besides the change from dip to sp and the superfluous double setting of textSize , there is no problem with your TextView code and it should work fine. 除了从dipsp的更改以及textSize的多余的double设置之外,您的TextView代码没有问题,它应该可以正常工作。 There some other small issues as possibly using @+id/qp_body instead and using match_parent instead of the deprecated fill_parent , but still it should be working. 还有其他一些小问题,可能使用@+id/qp_body代替,使用match_parent代替不推荐使用的fill_parent ,但仍然可以使用。

This is shooting in the dark, but check, if you are not somehow overwriting the value programatically in code. 这是在黑暗中进行的,但是请检查您是否没有以编程方式在代码中覆盖该值。 If not, you could always try to and see what happens: 如果没有,您可以随时尝试看看会发生什么:

TextView mTextView = (TextView) findViewById(R.id.qp_body);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);

Or using a dimension resource: 或使用维度资源:

TextView mTextView = (TextView) findViewById(R.id.qp_body);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.my_text_size));

您正在使用textsize中的dip ,而不是仅使用dpsp .dp和sp都可以使用,但通常在textview中,我们使用sp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM