简体   繁体   English

字体大小无法适应不同的屏幕大小

[英]Font Sizes Not Adjusting To Different Screen Sizes

I have read the Android articles about supporting different screen sizes via using "match_parent" and "wrap_content", as well as "sp" and "dp", and relative layout. 我已经阅读了有关使用“ match_parent”和“ wrap_content”以及“ sp”和“ dp”以及相对布局来支持不同屏幕尺寸的Android文章。 I have implemented all those suggestions, but still when I change the size of the screen to say, tablet, in the XML editor of Eclipse, the font sizes of the TextViews and Buttons do not seem to change at all to adapt to the the larger screen size. 我已经实现了所有这些建议,但是当我在Eclipse的XML编辑器中将屏幕尺寸更改为平板电脑时,TextViews和Buttons的字体大小似乎并没有改变以适应较大的尺寸。屏幕尺寸。 The same goes for smaller screens (such as 3.2 inch). 较小的屏幕(例如3.2英寸)也是如此。 Does anyone have any suggestions? 有没有人有什么建议?

to borrow heavily from @CSmith's answer (but not wanting to edit his answer, because it is valid): 从@CSmith的答案中大量借用(但不想编辑他的答案,因为它是有效的):

In a layout: 在布局中:

<TextView
 android:id="@+id/yourID"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="My String"
 style="@style/myStyle"
/> 

values/styles.xml: values / styles.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
 <style name="myStyle">
  <item name="android:textStyle">bold</item> 
  <item name="android:textColor">#FFFFFFFF</item> 
  <item name="android:textSize">@dimen/my_textsize</item> 
  <item name="android:shadowColor">#FF000000</item> 
  <item name="android:shadowDx">1</item> 
  <item name="android:shadowDy">-1</item> 
  <item name="android:shadowRadius">1</item> 
 </style>
</resources>

values/dimens.xml values / dimens.xml

<resources>    
    <dimen name="my_textsize">14sp</dimen>
</resources>

values-xlarge/dimens.xml values-xlarge / dimens.xml

<resources>    
    <dimen name="my_textsize">20sp</dimen>
</resources>

this will give you a 14sp font on normal devices, 20sp on x-large devices, and cuts down on duplication in separate XML files, since the only thing that's changing is the font-size . 这将使您在普通设备上具有14sp字体,在x大型设备上具有20sp字体,并且减少了在单独的XML文件中的重复,因为唯一改变的是font-size。

you can put anything that's going to be an sp/dp dimension in a dimens.xml file as a . 您可以将将成为sp / dp尺寸的任何内容作为放置在dimens.xml文件中。

In a layout: 在布局中:

<TextView android:id="@+id/yourID" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="My String" style="@style/myStyle" /> 

values/styles.xml: values / styles.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
 <style name="myStyle">
  <item name="android:textStyle">bold</item> 
  <item name="android:textColor">#FFFFFFFF</item> 
  <item name="android:textSize">14sp</item> 
  <item name="android:shadowColor">#FF000000</item> 
  <item name="android:shadowDx">1</item> 
  <item name="android:shadowDy">-1</item> 
  <item name="android:shadowRadius">1</item> 
 </style>
</resources>

values-xlarge/styles.xml: values-xlarge / styles.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
 <style name="myStyle">
  <item name="android:textStyle">bold</item> 
  <item name="android:textColor">#FFFFFFFF</item> 
  <item name="android:textSize">20sp</item> 
  <item name="android:shadowColor">#FF000000</item> 
  <item name="android:shadowDx">1</item> 
  <item name="android:shadowDy">-1</item> 
  <item name="android:shadowRadius">1</item> 
 </style>
</resources>

this will give you a 14sp font on normal devices, 20sp on x-large devices 这将在普通设备上为您提供14sp字体,在x大型设备上为您提供20sp字体

The point of using density-independent units such as 'sp' and 'dp' is that the text size actually remains the same from a user point of view, whatever the screen. 使用密度独立单位(例如“ sp”和“ dp”)的要点是,从用户的角度来看,无论屏幕是什么,文本大小实际上都保持不变。

So, when you change the screen in the XML editor, the displayed screen area changes but the text keeps the same size. 因此,当您在XML编辑器中更改屏幕时,显示的屏幕区域会更改,但文本将保持相同的大小。

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

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