java在TextView中更改textSize
在下面这段来自安卓 studio上的安卓应用程序的代码中,我有一个LinearLayout,其中包含一个文本视图,我想将其大小从100更改为20。但问题是,每当我将安卓:textSize=“100.0dip”更改为任何其他数字时,在我的手机上运行时,应用程序中的大小仍然保持不变
代码:
<ScrollView
安卓:id="@id/qp_body_wrapper"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:layout_above="@id/widget34"
安卓:layout_below="@id/widget99"
安卓:layout_marginBottom="5.0dip"
安卓:layout_marginTop="10.0dip"
安卓:clickable="true"
安卓:focusable="true"
安卓:foregroundGravity="right">
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@id/qp_body_wrapper_container"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:clickable="true"
安卓:focusable="true"
安卓:orientation="vertical">
<TextView
安卓:id="@id/qp_body"
style="@style/QuoteViewText"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:background="@drawable/dashboard_qod_bg"
安卓:clickable="true"
安卓:focusable="true"
安卓:gravity="right"
安卓:padding="10.0dip"
安卓:textAlignment="viewEnd"
安卓:textColor="#FFFFFF"
安卓:textSize="100.0dip"
tools:ignore="RtlCompat,RtlHardcoded" />
</LinearLayout>
</ScrollView>
下面是QuoteViewText样式:
<style name="QuoteViewText">
<item name="安卓:textSize">100.0dip</item>
<item name="安卓:typeface">serif</item>
<item name="安卓:textStyle">bold</item>
<item name="安卓:textColor">#fffcfcfc</item>
<item name="安卓:shadowColor">#ff102e46</item>
<item name="安卓:shadowDx">0.0</item>
<item name="安卓:shadowDy">1.0</item>
<item name="安卓:shadowRadius">2.0</item>
</style>
# 1 楼答案
文本大小以
sp
比例无关的像素测量所以,在你的情况下,你应该使用
相反
另外,别忘了样式是由布局中的属性覆盖的,在这种情况下,您应该从
TextView
中删除android:textSize
,只保留在样式中。此外,你应该从文本大小10开始,100是大的,从来没有使用过# 2 楼答案
提示:使用
dimens.xml
存储具体的值,以便更轻松、更清晰地管理代码除了从
dip
更改为sp
以及多余的textSize
双重设置之外,TextView
代码没有问题,应该可以正常工作。还有其他一些小问题,比如可能使用@+id/qp_body
代替,或者使用match_parent
代替不推荐的fill_parent
,但它仍然应该有效这是在黑暗中拍摄的,但是如果您没有以编程方式在代码中覆盖该值,请检查。如果没有,你可以尝试看看会发生什么:
或者使用维度资源:
# 3 楼答案
您正在将textview大小设置为2倍,一个是应用样式,另一个是应用textview的textsize。使用任何一个。对于文本,使用sp而不是dp来尊重用户的偏好
# 4 楼答案
您正在使用dip,而应该使用sp来显示textSize
你可以用下面的文件更新你的代码文件,它会解决你的问题
以下是QuoteViewText风格:
# 5 楼答案
在文本大小中使用dip,而不是使用dp或sp。dp和sp都可以工作,但通常在文本视图中,我们使用sp