Android-如何自动滚动文本视图以与editText位置对齐?
我正在尝试创建一个带有行号的记事本。我可以显示Android-如何自动滚动文本视图以与editText位置对齐?,android,scroll,textview,Android,Scroll,Textview,我正在尝试创建一个带有行号的记事本。我可以显示lineNumber,但问题是,只要editext到达屏幕末尾,textview就无法使用editext当前位置或lineNumber自动滚动 这里是XML代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" a
lineNumber
,但问题是,只要editext
到达屏幕末尾,textview就无法使用editext当前位置或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;
}
}
这是片段代码
<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;
}
}
公共类FileViewFragment扩展了片段{
文本视图行号;
编辑文本文件文本;
字符串fileName=“”;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气器。充气(R.layout.fragment\u file\u查看器,容器,false);
lineNumber=view.findviewbyd(R.id.lineNumber);
fileText=view.findviewbyd(R.id.fileText);
addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
int lines=fileText.getLineCount();
字符串linesNumber=“”;
对于onTextChanged方法中的(int i=1;i),设置新的行号文本后,必须计算EditText的当前Y位置,并使用其scrollTo方法将TextView滚动到该位置。EditText Y位置的计算如下:
int y = fileText.getLayout().getLineTop(fileText.getLineCount()) - (fileText.getHeight()) + (fileText.getPaddingBottom()) + (fileText.getPaddingTop());
lineNumber.scrollTo(0, Math.max(y, 0));
当然,如果您希望文本视图可滚动,则必须在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>
在您的情况下,您需要自定义一个扩展FrameLayout的视图组,并重写onMeasure方法,获取EditText width和LineNumber TextView width,如果宽度之和大于FrameLayout width,请更改LineNumber TextView marginTop。@Paradisell您可以提供一个例子还是代码?谢谢,伙计。这很有效。但是如果我调用back-up textview没有被回滚,他们的问题就出现了。检查我的新答案,它应该可以解决你的滚动问题。