Android 如何将EditText附加到TextView的末尾

Android 如何将EditText附加到TextView的末尾,android,terminal,console,android-edittext,textview,Android,Terminal,Console,Android Edittext,Textview,我正在编写一个类似终端的应用程序(实际上是一个ssh客户端)。我现在有一个TextView,它输出响应和一个EditText,固定在底部,用于键入命令 我正在尝试将EditText附加到TextView输出的末尾(动态),就像在真实的终端中一样: [TextView]root@whatever:~#[EditText] |command 你有什么想法吗 我将努力澄清我想要实现的目标 我无法设置固定的权重,因为随着更多的字节从流中到达,更多的文本被异步添加到TextView。LinearLayo

我正在编写一个类似终端的应用程序(实际上是一个ssh客户端)。我现在有一个
TextView
,它输出响应和一个
EditText
,固定在底部,用于键入命令

我正在尝试将
EditText
附加到
TextView
输出的末尾(动态),就像在真实的终端中一样:

[TextView]root@whatever:~#[EditText] |command
你有什么想法吗

我将努力澄清我想要实现的目标

我无法设置固定的权重,因为随着更多的字节从流中到达,更多的文本被异步添加到
TextView
。LinearLayout solutions没有在
TextView
输入的末尾设置
EditText
光标位置。我尝试了RelativeLayout toRightOf,但
EditText
只是在
TextView
填充时退出屏幕

我想要的是:


在代码中尝试这一点,根据需要为视图添加权重

   LinearLayout linearLayout=new LinearLayout(this);
   LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
   LayoutParams.WRAP_CONTENT);
   linearLayout.setLayoutParams(params);
   linearLayout.setWeightSum(1.0f);
   linearLayout.setOrientation(LinearLayout.HORIZONTAL);
   TextView textView=new TextView(this);
   EditText editText=new EditText(this);
   textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
   LayoutParams.WRAP_CONTENT,
   0.5f));
   editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
   LayoutParams.WRAP_CONTENT,
   0.5f));
   linearLayout.addView(textView);
   linearLayout.addView(editText);

为了避免在不同的屏幕大小上编辑文本被“吃掉”,最好使用布局权重。例如:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >

    <TextView
       android:id="@+id/yourTextView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1" />

    <EditText
       android:id="@+id/yourEditText"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1" />

    </LinearLayout>

这样,两个视图将具有相同的尺寸(长度、高度)。您可以使用layout_weight属性。如果要使TextView大于EditText,请将TextView的权重设置为0.25,EditText的权重设置为0.75。当然,反之亦然

编辑 如果您想拥有用户无法更改的静态文本,可以执行以下操作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   >

 <EditText
   android:id="@+id/yourEditText"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   />

 <TextView
   android:id="@+id/yourTextView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@id/yourEditText"
   android:layout_alignBaseline="@+id/yourEditText"
   android:text="C:\" />

 </RelativeLayout>


目前我无法测试它,但它应该会提示您如何操作。

通过在
文本视图
(etShell)上每次追加后更新
编辑文本
(etInput)位置来解决:


您尝试了什么?例如,使用水平方向的线性布局。将TextView和EditText放入其中。这取决于您需要什么,您还可以使用权重属性创建布局使用相对布局,并在该xml中添加textview。创建DynamicEditText并放置android:layout_toRightOf=“@+id/textview”感谢您的回答。不幸的是,由于异步添加了更多的文本,因此EditText在TextView中没有跟随文本的末尾。重量需要动态设置。我会补充更多细节。谢谢你的回答。但结果与上述答案相同。我需要EditText在TextView中跟随输入的末尾,而不是固定的权重。哦,你是说就像sombody在终端中放入文件路径一样,你想在之前给出一些静态符号或路径?像斜杠/例如开头的不可更改的斜杠?不太可能。考虑到换行,我只希望EditText位于TextView的最后一个字符之后。尝试了RelativeLayout,但编辑文本刚刚脱离屏幕。请看这里:TextView是动态的。类似的问题,但这家伙想在TextView的末尾添加一个按钮:
Layout layout = etShell.getLayout();
int pos = etShell.length();
int line = layout.getLineForOffset(pos);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
float x = layout.getPrimaryHorizontal(pos);
float y = baseline + ascent;
etInput.setMaxWidth(width - (int) x);
etInput.setX(x);
etInput.setY(y);