针对不同屏幕的Android文字大小
问题描述:
嘿,我知道有很多与这个主题有关的问题,我只需要一些澄清。针对不同屏幕的Android文字大小
我试图让我的应用在所有设备尺寸上看起来都差不多。我使用'sp'作为“textSize”的值类型,但它似乎没有做任何事情。它在ldpi和hdpi屏幕尺寸上看起来太大,在mdpi和xxhdpi上太小。它看起来像我希望它看起来在hdpi和xhdpi屏幕上。
我在AndroidManifest文件中启用了“屏幕支持”。我遇到的问题是值文件夹。
这是目前我有什么有: (我还没有做出我自己的任何修改文件夹结构,我所知道的)
res
-Values
--dimens.xml
--styles.xml
--strings.xml
-Values-v11
--styles.xml
-Values-v14
--styles.xml
-Values-w820dp
--dimens.xml
难道我只是做一个新值文件夹ldpi,hdpi,xhdpi,xxhdpi等?然后为每个创建一个新的dimens.xml文件?那之后呢?
这是我的AndroidManifest.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>
我使用的LinearLayout如果这意味着什么。让我知道你是否需要别的东西。感谢您提供的任何帮助。
答
我不知道我是否可以得到这个问题,但你可以尝试用相当大的尺寸this.Starting在textvew并减少它的文字,直到它符合..
`
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();
}`
你解决了你的问题吗? – OnePunchMan