不同屏幕的Android文本大小
嘿,我知道关于这个话题有很多问题,我只是需要澄清一下 我正试图让我的应用程序在所有大小的设备上看起来都一样。我使用“sp”作为“textSize”的值类型,但它似乎没有任何作用。它在ldpi和hdpi屏幕尺寸上看起来太大,在mdpi和xxhdpi上看起来太小。它看起来就像我希望它在hdpi和xhdpi屏幕上的样子 我已经在我的AndroidManifest文件上启用了“屏幕支持”。我遇到的问题是“值”文件夹 这些是我目前所拥有的: (据我所知,我尚未对文件夹结构进行任何修改) 我是否为ldpi、hdpi、xhdpi、xxhdpi等创建一个新的值文件夹。?然后为每个文件创建一个新的dimens.xml?那之后呢 以下是我的AndroidManifest.xml文件(应用程序标记最小化):不同屏幕的Android文本大小,android,text,screens,Android,Text,Screens,嘿,我知道关于这个话题有很多问题,我只是需要澄清一下 我正试图让我的应用程序在所有大小的设备上看起来都一样。我使用“sp”作为“textSize”的值类型,但它似乎没有任何作用。它在ldpi和hdpi屏幕尺寸上看起来太大,在mdpi和xxhdpi上看起来太小。它看起来就像我希望它在hdpi和xhdpi屏幕上的样子 我已经在我的AndroidManifest文件上启用了“屏幕支持”。我遇到的问题是“值”文件夹 这些是我目前所拥有的: (据我所知,我尚未对文件夹结构进行任何修改) 我是否为ldpi、
如果有什么意义的话,我用的是线性布局。如果你还需要我帮忙,请告诉我。感谢您提供的任何帮助。我不确定是否能回答这个问题,但您可以试试这个。首先,文本VEW中的文本大小要相当大,然后再减小,直到合适为止 `
publicstaticvoid setTextSizeToFitTextView(TextView-tv)
/*
*使用指定的宽度和高度调整文本大小
*@参数宽度
*@param高度
*/
int height=tv.getHeight();
int width=tv.getWidth();
float mTextSize=tv.getTextSize();
CharSequence text=tv.getText();
//如果视图没有尺寸标注或没有尺寸标注,请不要调整大小
//正文
如果(text==null | | | text.length()==0 | | height),您的问题解决了吗?
res
-Values
--dimens.xml
--styles.xml
--strings.xml
-Values-v11
--styles.xml
-Values-v14
--styles.xml
-Values-w820dp
--dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.suckatprogramming.coach"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application... />
</manifest>
public static void setTextSizeToFitTextView(TextView tv)
/*
* Resize the text size with specified width and height
* @param width
* @param height
*/
int height = tv.getHeight();
int width = tv.getWidth();
float mTextSize = tv.getTextSize();
CharSequence text = tv.getText();
// Do not resize if the view does not have dimensions or there is no
// text
if (text == null || text.length() == 0 || height <= 0 || width <= 0
|| mTextSize == 0) {
return;
}
// Get the text view's paint object
TextPaint textPaint = tv.getPaint();
float targetTextSize = 50.0f;
// Get the required text height
int textHeight = getTextHeight(text, textPaint, width, targetTextSize);
// Until we either fit within our text view or we had reached our min
// text size, incrementally try smaller sizes
while (textHeight >= height) {
targetTextSize = targetTextSize - 2;
textHeight = getTextHeight(text, textPaint, width, targetTextSize);
}
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize);
// textPaint.setTextSize(targetTextSize);
}
// Set the text size of the text paint object and use a static layout to
// render text off screen before measuring
private static int getTextHeight(CharSequence source,
TextPaint originalPaint, int width, float textSize) {
// Update the text paint object
TextPaint paint = new TextPaint(originalPaint);
paint.setTextSize(textSize);
// Measure using a static layout
StaticLayout layout = new StaticLayout(source, paint, width,
Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
return layout.getHeight();
}`