在多个屏幕尺寸支持文本 [英] Support text in multiple screen size

查看:131
本文介绍了在多个屏幕尺寸支持文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了许多相关的话题,但话题主要围绕图像。我明白这一点: http://developer.android.com/guide/practices/screens_support。 HTML 但我真的不明白的字体动态调整的想法。

有关的图像没有任何问题,我用的是LDPI,MDPI,华电国际和xhdpi。不过,对于字体,我不知道该如何调整它们的大小。有人吗?

作为后面

我的codeS:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:ID =@ + ID / layoutHeader
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/标题图片
机器人:方向=横向><的TextView
    机器人:ID =@ + ID / headerTitle
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:文字颜色=#FFF
    机器人:TEXTSIZE =28dp
机器人:文字样式=大胆
    机器人:文本=一些随机的横幅标题
    风格=@风格/ headerTextStyle
 /><的ImageButton
    机器人:ID =@ + ID / btnPlus
    机器人:layout_width =50dp
    机器人:layout_height =50dp
    机器人:layout_alignParentRight =真
    机器人:layout_alignParentTop =真
    机器人:layout_marginRight =5DP
    机器人:layout_marginTop =6DP
    机器人:背景=#0000
    机器人:scaleType =centerCrop
    机器人:SRC =@绘制/加/><的ImageButton
    机器人:ID =@ + ID / btnInfo
    机器人:layout_width =50dp
    机器人:layout_height =50dp
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:layout_marginTop =6DP
    机器人:背景=#0000
    机器人:scaleType =centerCrop
    机器人:SRC =@绘制/信息/><的ImageButton
    机器人:ID =@ + ID / btnGuide
    机器人:layout_width =50dp
    机器人:layout_height =50dp
    机器人:layout_centerVertical =真
    机器人:layout_toLeftOf =@ + ID / btnPlus
    机器人:背景=#0000
    机器人:scaleType =centerCrop
    机器人:SRC =@绘制/引导/>< / RelativeLayout的>


解决方案

我会把文字大小在样式资源,然后用另一种样式的资源文件的屏幕尺寸。

所以你必须RES /价值观大/ style.xml,RES /值小/ style.xml等等。每个这些样式将包含该项目具有不同的DP值的文字大小。请记住,华电国际,MDPI,LDPI等有像素密度做的,而不是屏幕尺寸。

因此​​,举例来说,如果在res /价值/ style.xml您有:

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
<样式名称=headerTextStyle>
    <项目名称=机器人:TEXTSIZE> 28dp< /项目>
   ...
< /风格>
< /资源>

在其他资源文件,假设为大屏幕(RES /价值观大/ style.xml):

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
<样式名称=headerTextStyle>
    <项目名称=机器人:TEXTSIZE> 46dp< /项目>
   ...
< /风格>
< /资源>

然后当然这种风格应用到元素,系统将选择基于设备的屏幕大小。您可以提供在同一文件的多个样式,一个标题文本,一个用于常规文本正确的 - 不管不同类型的文本需要,类似CSS。

另外,你可以复制你的布局文件中类似的专职资源目录,如RES /布局大/ layout.xml,RES /布局小/ layout.xml并在每个不同文本的大小,但这将被复制更多的XML不必要的。

I have researched on many relevant topics but the topics mainly revolve around images. I had understand this: http://developer.android.com/guide/practices/screens_support.html but I don't really get the idea of dynamic resizing for the fonts.

For images there's no problem, I used the ldpi, mdpi, hdpi and xhdpi. However for the fonts, I do not know how to resize them. Anyone?

My codes is as followed:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/layoutHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/headerimage"
android:orientation="horizontal">

<TextView
    android:id="@+id/headerTitle"
    android:layout_width ="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textColor="#FFF"
    android:textSize="28dp"
android:textStyle="bold"
    android:text="Some Random Banner Title"
    style="@style/headerTextStyle"
 />

<ImageButton
    android:id="@+id/btnPlus"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="5dp"
    android:layout_marginTop="6dp"
    android:background="#0000"
    android:scaleType="centerCrop"
    android:src="@drawable/plus" />

<ImageButton
    android:id="@+id/btnInfo"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="6dp"
    android:background="#0000"
    android:scaleType="centerCrop"
    android:src="@drawable/info" />

<ImageButton
    android:id="@+id/btnGuide"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/btnPlus"
    android:background="#0000"
    android:scaleType="centerCrop"
    android:src="@drawable/guide" />

</RelativeLayout>

解决方案

I would put the text sizes in a style resource, then use alternate style resource files for screen sizes..

so you will have res/values-large/style.xml, res/values-small/style.xml, etc. Each of those styles will contain the item for the text size with different dp values. Remember, hdpi, mdpi, ldpi etc have to do with pixel density, not screen size.

So for instance, if in res/values/style.xml you have:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="headerTextStyle" >
    <item name="android:textSize">28dp</item>
   ...
</style>
</resources>

in another resource file, suppose for large screen (res/values-large/style.xml) :

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="headerTextStyle" >
    <item name="android:textSize">46dp</item>
   ...
</style>
</resources>

Then of course apply this style to the element, and the system will pick the correct one based on the device's screen size.. You can provide multiple styles in the same file, one for header text, one for regular text - whatever distinct types of text you need, similar to CSS.

Alternatively, you could duplicate your layout file in similarly qualified resource directories, like res/layout-large/layout.xml, res/layout-small/layout.xml and have different text sizes in each, but this will be duplicating more xml unnecessarily.

这篇关于在多个屏幕尺寸支持文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆