我在使用Textview时遇到了一些问题。我已经为我的应用编写了这段代码。如果有必要,我希望能够捏一下以缩放和滚动Textview。我编写了用于缩放的代码,它可以正常工作,但是如果我为textview的滚动插入代码,则它不起作用,但是滚动可以正常工作。如果删除滚动代码,则可以进行缩放,但是如果文本对于页面而言太长,则无法滚动。 我的代码有什么问题?
布局代码:
<TextView
android:id="@+id/tv_testo_canzone"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="40dp"
android:layout_marginTop="90dp"
android:scrollbars="vertical"
android:text="TextView"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
主要代码:
tv_testo_canzone = (TextView) findViewById(R.id.tv_testo_canzone);
tv_titolo = (TextView) findViewById(R.id.tv_titolo_canz);
tv_artista = (TextView) findViewById(R.id.tv_artista_canz);
tv_testo_canzone.setMovementMethod(new ScrollingMovementMethod());
linearLayout1 = (RelativeLayout) findViewById(R.id.layout_canzone);
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open(nomeTxt)));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
text.append(mLine);
text.append('\n');
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
tv_testo_canzone.setText((CharSequence) text);
}
答案 0 :(得分:0)
代码没有任何问题,使用ScrollView时,所有子视图都不会收到手势,因为它们都是由父ScrollView处理的。
答案 1 :(得分:-1)
description.setOnTouchListener((v, event) -> {
// TODO Auto-generated method stub
if (event.getPointerCount() == 1) {
Log.d("Scroll", "1-pointer touch");
v.getParent().requestDisallowInterceptTouchEvent(false);
}
if (event.getPointerCount() == 2) {
Log.d("Zoom", "2-pointer touch");
int action = event.getAction();
int mainaction = action & MotionEvent.ACTION_MASK;
if (mainaction == MotionEvent.ACTION_POINTER_DOWN) {
mBaseDist = getDistance(event);
mBaseRatio = mRatio;
} else {
float scale = (getDistance(event) - mBaseDist) / STEP;
float factor = (float) Math.pow(2, scale);
mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * factor));
description.setTextSize(mRatio + 15);
}
v.getParent().requestDisallowInterceptTouchEvent(true);
}
return true;
});
}
private int getDistance(MotionEvent event) {
int dx = (int) (event.getX(0) - event.getX(1));
int dy = (int) (event.getY(0) - event.getY(1));
return (int) (Math.sqrt(dx * dx + dy * dy));
}