gpt4 book ai didi

android - 为什么ScrollView不把TextView作为一个整体来显示?

转载 作者:行者123 更新时间:2023-11-30 02:33:19 24 4
gpt4 key购买 nike

我有一个包含 TextView 的 ScrollBar,但 TextView 没有完全显示。

如何使 TextView 扩展以填充 ScrollView?

我有一个包含 TextView 的 ScrollBar,但 TextView 没有完全显示。

如何使 TextView 扩展以填充 ScrollView?

Screenshot

我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootEditLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >

<ScrollView
android:id="@+id/ScrollViewDescribtion"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_margin="5dip"
android:layout_weight="99"
android:clipChildren="true"
android:fadeScrollbars="false"
android:fillViewport="true"
android:foregroundGravity="fill_vertical" >

<LinearLayout
android:id="@+id/DescribtionLinearLayour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<com.iman.marashi.makeupandbeauty.TextViewEx
android:id="@+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dip"
android:ellipsize="marquee"
android:gravity="right"
android:inputType="textMultiLine"
android:text="txtDescription" />
</LinearLayout>
</ScrollView>

<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/sep_line1" >
</ImageView>


</LinearLayout>

我发现,当我使用 TexView 而不是 TextViewEx 时,可以完全填充到 ScrollView,但不需要对齐。

TextViewEx代码:

public class TextViewEx extends TextView {
private Paint paint = new Paint();
private String[] blocks;
private float spaceOffset = 0;
private float horizontalOffset = 0;
private float verticalOffset = 0;
private float horizontalFontOffset = 0;
private float dirtyRegionWidth = 0;
private boolean wrapEnabled = false;
int left, top, right, bottom = 0;
private Align _align = Align.RIGHT;
private float strecthOffset;
private float wrappedEdgeSpace;
private String block;
private String wrappedLine;
private String[] lineAsWords;
private Object[] wrappedObj;
public boolean hyphenate = false;
private String syllableSeparator = "";
private Bitmap cache = null;
private boolean cacheEnabled = false;

public TextViewEx(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// set a minimum of left and right padding so that the texts are not too
// close to the side screen
this.setPadding(10, 0, 10, 0);
}

public TextViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
this.setPadding(10, 0, 10, 0);
}

public TextViewEx(Context context) {
super(context);
this.setPadding(10, 0, 10, 0);
}

@Override
public void setPadding(int left, int top, int right, int bottom) {

super.setPadding(left + 10, top, right + 10, bottom);
}

@Override
public void setDrawingCacheEnabled(boolean cacheEnabled) {
this.cacheEnabled = cacheEnabled;
}

public void setText(String st, boolean wrap) {
wrapEnabled = wrap;
super.setText(st);
}

public void setTextAlign(Align align) {
_align = align;
}

@SuppressLint({ "NewApi", "DrawAllocation" })
@Override
protected void onDraw(Canvas canvas) {
// If wrap is disabled then,
// request original onDraw
if (!wrapEnabled) {
super.onDraw(canvas);
return;
}
// Active canas needs to be set
// based on cacheEnabled
Canvas activeCanvas = null;
// Set the active canvas based on
// whether cache is enabled
if (cacheEnabled) {
if (cache != null) {
// Draw to the OS provided canvas
// if the cache is not empty
canvas.drawBitmap(cache, 0, 0, paint);
return;
} else {
// Create a bitmap and set the activeCanvas
// to the one derived from the bitmap
cache = Bitmap.createBitmap(getWidth(), getHeight(),
Config.ARGB_4444);
activeCanvas = new Canvas(cache);
}
} else {
// Active canvas is the OS
// provided canvas
activeCanvas = canvas;
}
// Pull widget properties
paint.setColor(getCurrentTextColor());
paint.setTypeface(getTypeface());
paint.setTextSize(getTextSize());
paint.setTextAlign(_align);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
// minus out the paddings pixel
dirtyRegionWidth = getWidth() - getPaddingLeft() - getPaddingRight();
int maxLines = Integer.MAX_VALUE;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
maxLines = getMaxLines();
}
int lines = 1;
blocks = getText().toString().split("((?<=\n)|(?=\n))");
verticalOffset = horizontalFontOffset = getLineHeight() - 0.5f; // Temp
// fix
spaceOffset = paint.measureText(" ");
for (int i = 0; i < blocks.length && lines <= maxLines; i++) {
block = blocks[i];
horizontalOffset = 0;
if (block.length() == 0) {
continue;
} else if (block.equals("\n")) {
verticalOffset += horizontalFontOffset;
continue;
}
block = block.trim();
if (block.length() == 0) {
continue;
}
wrappedObj = TextJustifyUtils
.createWrappedLine(block, paint, spaceOffset,
dirtyRegionWidth, hyphenate, syllableSeparator);
wrappedLine = ((String) wrappedObj[0]);
wrappedEdgeSpace = (Float) wrappedObj[1];
Integer charCounter = (Integer) wrappedObj[2];
lineAsWords = wrappedLine.split(" ");
strecthOffset = wrappedEdgeSpace != Float.MIN_VALUE ? wrappedEdgeSpace
/ (lineAsWords.length - 1)
: 0;
for (int j = 0; j < lineAsWords.length; j++) {
String word = lineAsWords[j];
if (lines == maxLines && j == lineAsWords.length - 1) {
activeCanvas.drawText("...", horizontalOffset,
verticalOffset, paint);
} else if (j == 0) {
// if it is the first word of the line, text will be drawn
// starting from right edge of textview
if (_align == Align.RIGHT) {
activeCanvas.drawText(word, getWidth()
- (getPaddingRight()), verticalOffset, paint);
// add in the paddings to the horizontalOffset
horizontalOffset += getWidth() - (getPaddingRight());
} else {
activeCanvas.drawText(word, getPaddingLeft(),
verticalOffset, paint);
horizontalOffset += getPaddingLeft();
}
} else {
activeCanvas.drawText(word, horizontalOffset,
verticalOffset, paint);
}
if (_align == Align.RIGHT)
horizontalOffset -= paint.measureText(word) + spaceOffset
+ strecthOffset;
else
horizontalOffset += paint.measureText(word) + spaceOffset
+ strecthOffset;
}
lines++;
if (blocks[i].length() > 0) {
blocks[i] = blocks[i].substring(charCounter);
verticalOffset += blocks[i].length() > 0 ? horizontalFontOffset
: 0;
i--;
}
}
if (cacheEnabled) {
// Draw the cache onto the OS provided
// canvas.
canvas.drawBitmap(cache, 0, 0, paint);
}
}

public void setHyphenate(boolean hyphenate, String syllableSeparator) {
this.hyphenate = hyphenate;
this.syllableSeparator = syllableSeparator;
}
}

最佳答案

我通过改变这个方法解决了这个问题:

@Override
public void setPadding(int left, int top, int right, int bottom) {
DisplayMetrics metrics = getContext().getResources()
.getDisplayMetrics();
super.setPadding((int) ((left + (metrics.density * 10))), top,
(int) (right + (metrics.density * 10)),
(int) (bottom + (metrics.heightPixels * 1.5)));
Log.i("Log", "metrics : " + metrics);
}

关于android - 为什么ScrollView不把TextView作为一个整体来显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26979951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com