我有layout
<ScrollView>
<TextView android:id="@+id/textView" />
</ScrollView>
当我尝试设置太长的文字时,此Textview
不显示该内容。
//OnCreate
//...
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("..."); //Here is a text with more than 2500
//chars and at least have 10 \n char
//(it means has at least 10 paragraph)
如何显示该文字?
编辑一个:
即使我为background
设置TextView
,TextView
也没有显示背景
答案 0 :(得分:14)
你可以设置
android:maxLines="1"
android:ellipsize="end"
到你的textview,所以长于你的textview的文本将被隐藏。
答案 1 :(得分:3)
由于android:singleLine="true"
属性已被弃用,因此请将以下属性设置为您的TextView:
android:ellipsize="end"
android:maxLines="1"
然后,TextView将仅显示适合您的TextView的文本,后跟省略号...
答案 2 :(得分:1)
使用此:
EditText thumbnailView = (EditText) findViewById(R.id.enterBearingNo_editText_id);
TextView messageView = (TextView) findViewById(R.id.string2);
String text = "LargeText";
Display display = getWindowManager().getDefaultDisplay();
FlowTextHelper.tryFlowText(text, thumbnailView, messageView, display);
FlowTextHelper.class
class FlowTextHelper {
static boolean mNewClassAvailable;
static {
if (Integer.valueOf(Build.VERSION.SDK) >= 8) { // Froyo 2.2, API level 8
mNewClassAvailable = true;
}
// Also you can use this trick if you don't know the exact version:
/*
* try {
* Class.forName("android.text.style.LeadingMarginSpan$LeadingMarginSpan2"
* ); mNewClassAvailable = true; } catch (Exception ex) {
* mNewClassAvailable = false; }
*/
}
public static void tryFlowText(String text, View thumbnailView,
TextView messageView, Display display) {
// There is nothing I can do for older versions, so just return
if (!mNewClassAvailable)
return;
// Get height and width of the image and height of the text line
thumbnailView.measure(display.getWidth(), display.getHeight());
int height = thumbnailView.getMeasuredHeight();
int width = thumbnailView.getMeasuredWidth();
float textLineHeight = messageView.getPaint().getTextSize();
// Set the span according to the number of lines and width of the image
int lines = (int) Math.round(height / textLineHeight);
// For an html text you can use this line: SpannableStringBuilder ss =
// (SpannableStringBuilder)Html.fromHtml(text);
SpannableString ss = new SpannableString(text);
ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageView.setText(ss);
// Align the text with the image by removing the rule that the text is
// to the right of the image
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) messageView
.getLayoutParams();
int[] rules = params.getRules();
rules[RelativeLayout.RIGHT_OF] = 0;
}
public static void tryFlowTextPrice(String text, TextView messageView,
Display display) {
// There is nothing I can do for older versions, so just return
if (!mNewClassAvailable)
return;
// Get height and width of the image and height of the text line
// thumbnailView.measure(display.getWidth(), display.getHeight());
// int height = thumbnailView.getMeasuredHeight();
// int width = thumbnailView.getMeasuredWidth();
float textLineHeight = messageView.getPaint().getTextSize();
// Set the span according to the number of lines and width of the image
// int lines = (int) Math.round(height / textLineHeight);
// For an html text you can use this line: SpannableStringBuilder ss =
// (SpannableStringBuilder)Html.fromHtml(text);
SpannableString ss = new SpannableString(text);
// ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(),
// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageView.setText(ss);
// Align the text with the image by removing the rule that the text is
// to the right of the image
// LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
// messageView
// .getLayoutParams();
// int[] rules = params.getRules();
// rules[RelativeLayout.RIGHT_OF] = 0;
}
}
答案 3 :(得分:1)
我不知道这是否对您有帮助,但是在developer.android上看到了这一点...
在Android 8.0(API级别26)及更高版本中,您可以指示TextView根据TextView的特征和边界自动扩大或缩小文本大小以填充其布局。通过此设置,可以更轻松地优化具有动态内容的不同屏幕上的文本大小。
android:autoSizeTextType="uniform"
可以通过以下几种三种方法来设置TextView的自动大小调整:
================================================ ==