AndroidStudio当Test View中的内容超过文本框本身的大小,想通过上下滑动看到所有内容,找了很多种方法,这种方法最多人推荐,但是并不能实现
这是XML代码
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询内容:"
android:textSize="20sp"
android:textColor="@color/white"/>
<TextView
android:id="@+id/tv_find"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_marginTop="10dp"
android:background="@drawable/test_kuang"
android:fadeScrollbars="false"
android:gravity="center"
android:lineSpacingMultiplier="1.5"
android:maxLines="10"
android:scrollbars="vertical"
android:text=""
android:textSize="18sp"
android:theme="@style/MyEditText" />
</LinearLayout>
</ScrollView>
Java代码
textView.setMovementMethod(new ScrollingMovementMethod());
请问有什么方法可以实现
我这是一个简易版本的滚动条,代码很少,你可以复制粘贴到你的AndroidStudio中尝试一下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:hint="Input your message..."
android:id="@+id/mEditView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:id="@+id/mScrollView"
android:background="#4CAF50"
android:layout_width="match_parent"
android:layout_height="50dp"
android:scrollbars="vertical">
<TextView
android:id="@+id/mTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</ScrollView>
</LinearLayout>
java代码:
package com.application.practiceone;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView textView;
private ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.mEditView);
textView=findViewById(R.id.mTextView);
scrollView=findViewById(R.id.mScrollView);
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
textView.setText(editText.getText());
return false;
}
});
}
}