文本大小和不同的Android屏幕大小

文本大小和不同的Android屏幕大小

我知道,它已经讨论了1000次,但我不能调整不同屏幕大小的文本大小。我尝试在我的自定义样式中使用‘sp’作为大小单位:

<style name="CustumButtonStyle" parent="@android:style/Widget.Button">
    ...
    <item name="android:textSize">30sp</item>
    ...</style>

在2.7 QVGA中,它看起来没问题:

但是在WSVGA中,它看起来是这样的:

我尝试使用“sp”和“dp”,结果相同。

你能解释一下如何使这些按钮在任何屏幕上看起来都一样吗?

完全自定义按钮样式

<style name="CustumButtonStyle" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/custom_button</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:gravity">center</item>
    <item name="android:textSize">30sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">2</item></style>

在我的应用主题中

<item name="android:buttonStyle">@style/CustumButtonStyle</item>

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/RelativeLayout1"android:layout_width="fill_parent"android:background="@drawable/grid"android:gravity="center"android:orientation="vertical" android:layout_height="fill_parent"><Button
    android:id="@+id/buttonContinue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="@string/continue_game" android:layout_marginTop="3dp" android:layout_marginBottom="3dp"/><Button
    android:id="@+id/buttonNewGame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttonContinue"
    android:layout_alignRight="@+id/buttonContinue"
    android:layout_below="@+id/buttonContinue"
慕的地8271018
浏览 310回答 3
3回答

拉风的咖菲猫

我想在这条线上回复已经太晚了。但是我想分享我的想法或方法来解决不同分辨率设备上的文本大小问题。许多android开发者网站建议我们必须使用SP文本大小单元,用于处理不同分辨率设备的文本大小。但我总是无法得到预期的结果。因此,我找到了一个解决方案,我正在使用从我的上一个4-5项目和它的工作罚款。根据我的建议,您必须为每个分辨率设备放置文本大小,这是有点繁琐的工作,但它将满足您的要求。每个开发人员都必须听取如下所示的比率4:6:8:12(分别为h:xh:xxh:xxxh)..现在你的项目里力士文件夹,你必须创建4个文件夹与酒窝文件。RES/Value-hdpi/ddens.xmlRES/Value-xhdpi/ddens.xmlRES/Value-xxhdpi/ddens.xmlres/value-xxxhdpi/ddens.xml现在,在dudens.xml文件中,您必须放置文本大小。我给你看的代码是值-hdpi,类似地,您必须为其他解析值/dwens.xml文件放置代码。<?xml&nbsp;version="1.0"&nbsp;encoding="utf-8"?><resources> &nbsp;&nbsp;&nbsp;<dimen&nbsp;name="text_size">4px</dimen></resources>对于其他决议来说,这就像xhdpi*6PXxxhdpi*8%,xxxhdpi*12便士。这是根据我上面写的比例(3:4:6:8:12)计算的。让我们讨论其他文本大小与上述比例的例子。如果您想在hdpi中取12 px的文本大小,那么在其他分辨率中应该是hdpi:12 pxxhdpi:18 pxxxhdpi:24 pxxxxhdpi:36 px这是实现所有决议所需文本大小的简单解决方案。我没有考虑数值-MDPI解析器在这里。如果有人想要包含此决议的文本大小,则口粮如下3:4:6:8:12..如果有任何疑问,请告诉我。希望它能帮到你们。

湖上湖

有时候,最好只有三个选择&nbsp;style="@android:style/TextAppearance.Small"使用小屏幕和大屏幕来区分正常屏幕大小。<TextView &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:id="@+id/TextViewTopBarTitle" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="wrap_content" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="wrap_content" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style="@android:style/TextAppearance.Small"/>对于正常情况,您不必指定任何内容。<TextView &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:id="@+id/TextViewTopBarTitle" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="wrap_content" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="wrap_content"/>使用它,您可以避免测试和指定不同屏幕大小的尺寸。
打开App,查看更多内容
随时随地看视频慕课网APP