Android - 如何自动滚动 textView 以与 editText 位置对齐?
Android - how to auto scroll textView to align with editText position?
我正在尝试用行号创建一个记事本。我能够显示 lineNumber
,但问题是一旦 editext
到达屏幕末尾,textview 就不会使用 editext current position
或 lineNumber
自动滚动。
这里是XML代码
<RelativeLayout 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">
<TextView
android:id="@+id/lineNumber"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#eee"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="1"
android:textSize="18sp"
tools:layout_editor_absoluteY="12dp"
/>
<EditText
android:id="@+id/fileText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="@+id/lineNumber"
android:ems="10"
android:padding="10dp"
android:gravity="left|top"
android:inputType="textMultiLine"
android:scrollbars="vertical"/>
</RelativeLayout>
这是片段代码
public class FileViewFragment extends Fragment {
TextView lineNumber;
EditText fileText;
String fileName = "";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_file_viewer, container, false);
lineNumber = view.findViewById(R.id.lineNumber);
fileText = view.findViewById(R.id.fileText);
fileText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int lines = fileText.getLineCount();
String linesNumber = "";
for (int i = 1; i <= lines; i++) {
linesNumber = linesNumber + i + "\n";
}
lineNumber.setText(linesNumber);
}
@Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
}
那么,如何使用编辑文本位置自动滚动文本视图?
在您的 onTextChanged 方法中,在您设置了新的 lineNumber 文本之后,您必须计算 EditText 的当前 Y 位置并使用其 [= 将 TextView 滚动到该位置16=]scrollTo 方法。 EditText Y位置的计算如下:
int y = fileText.getLayout().getLineTop(fileText.getLineCount()) - (fileText.getHeight()) + (fileText.getPaddingBottom()) + (fileText.getPaddingTop());
lineNumber.scrollTo(0, Math.max(y, 0));
当然,如果您希望 TextView 可滚动,则必须在 onCreateView 方法中添加以下代码行:
lineNumber.setMovementMethod(new ScrollingMovementMethod());
要解决滚动问题,您必须更改 xml 文件并将 EditText 和 TextView 放在单个父视图中,并包装到滚动视图内的内容中,这样现在整个视图都可以滚动.下面是新的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:background="#eee"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/containerRl"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lineNumber"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#eee"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="1"
android:textSize="18sp"
tools:layout_editor_absoluteY="12dp" />
<EditText
android:id="@+id/fileText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="@+id/lineNumber"
android:ems="10"
android:padding="10dp"
android:gravity="left|top"
android:inputType="textMultiLine"
android:scrollbars="vertical"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
并删除我之前回答中的代码行。
我正在尝试用行号创建一个记事本。我能够显示 lineNumber
,但问题是一旦 editext
到达屏幕末尾,textview 就不会使用 editext current position
或 lineNumber
自动滚动。
这里是XML代码
<RelativeLayout 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">
<TextView
android:id="@+id/lineNumber"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#eee"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="1"
android:textSize="18sp"
tools:layout_editor_absoluteY="12dp"
/>
<EditText
android:id="@+id/fileText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="@+id/lineNumber"
android:ems="10"
android:padding="10dp"
android:gravity="left|top"
android:inputType="textMultiLine"
android:scrollbars="vertical"/>
</RelativeLayout>
这是片段代码
public class FileViewFragment extends Fragment {
TextView lineNumber;
EditText fileText;
String fileName = "";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_file_viewer, container, false);
lineNumber = view.findViewById(R.id.lineNumber);
fileText = view.findViewById(R.id.fileText);
fileText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int lines = fileText.getLineCount();
String linesNumber = "";
for (int i = 1; i <= lines; i++) {
linesNumber = linesNumber + i + "\n";
}
lineNumber.setText(linesNumber);
}
@Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
}
那么,如何使用编辑文本位置自动滚动文本视图?
在您的 onTextChanged 方法中,在您设置了新的 lineNumber 文本之后,您必须计算 EditText 的当前 Y 位置并使用其 [= 将 TextView 滚动到该位置16=]scrollTo 方法。 EditText Y位置的计算如下:
int y = fileText.getLayout().getLineTop(fileText.getLineCount()) - (fileText.getHeight()) + (fileText.getPaddingBottom()) + (fileText.getPaddingTop());
lineNumber.scrollTo(0, Math.max(y, 0));
当然,如果您希望 TextView 可滚动,则必须在 onCreateView 方法中添加以下代码行:
lineNumber.setMovementMethod(new ScrollingMovementMethod());
要解决滚动问题,您必须更改 xml 文件并将 EditText 和 TextView 放在单个父视图中,并包装到滚动视图内的内容中,这样现在整个视图都可以滚动.下面是新的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:background="#eee"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/containerRl"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lineNumber"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#eee"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="1"
android:textSize="18sp"
tools:layout_editor_absoluteY="12dp" />
<EditText
android:id="@+id/fileText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="@+id/lineNumber"
android:ems="10"
android:padding="10dp"
android:gravity="left|top"
android:inputType="textMultiLine"
android:scrollbars="vertical"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
并删除我之前回答中的代码行。