Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在edittext下以编程方式创建新的textview_Android_Textview - Fatal编程技术网

Android 在edittext下以编程方式创建新的textview

Android 在edittext下以编程方式创建新的textview,android,textview,Android,Textview,我有一个带有edittext和按钮的活动,前一个具有属性 android:layout_weight="1" 当我按下按钮时,会以编程方式创建一个新的textview。我希望新的文本视图显示在编辑文本下。 使用我的代码,每个文本视图都在按钮的右侧创建。我怎样才能让它出现在我想要的地方,在一条新的线上? 我想,因为textview是由代码创建的,所以唯一的方法是通过编程 这是我的activity.xml <LinearLayout xmlns:android="http://schemas

我有一个带有edittext和按钮的活动,前一个具有属性

android:layout_weight="1"
当我按下按钮时,会以编程方式创建一个新的textview。我希望新的文本视图显示在编辑文本下。 使用我的代码,每个文本视图都在按钮的右侧创建。我怎样才能让它出现在我想要的地方,在一条新的线上? 我想,因为textview是由代码创建的,所以唯一的方法是通过编程

这是我的activity.xml

<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="horizontal"
    android:id="@+id/new_formulas"
    >
    <EditText
        android:layout_weight="1"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/add_hint"
        android:id="@+id/add_hint"
     />
    <Button android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add"
        android:id="@+id/add"
        />
</LinearLayout>
}

我尝试使用addRule和RelativeLayout进行此操作,但不起作用。

使用此布局:

<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"
    android:orientation="horizontal"
    android:id="@+id/new_formulas"
    >

    <Button
        android:textSize="30dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add"
        android:id="@+id/add"
        />
    <EditText
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/add_hint"
        android:id="@+id/add_hint"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/add"
        />
    <LinearLayout
        android:id="@+id/hints"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/add_hint"
        android:orientation="vertical"/>
</RelativeLayout>

现在,每个孩子
TextView
都可以添加到
提示中
线性布局
,就像上面所做的一样,无需使用规则

重要提示:使用
LayoutParams
时,请始终从父布局使用它。例如,这里应该是
LinearLayout.LayoutParams

试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/new_formulas"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/add_hint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/add_hint"
            android:textSize="30dp" />

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add"
            android:textSize="30dp" />
    </LinearLayout>

</LinearLayout>

你可以做类似的事情

private void createNewTextView(String text) {
    final TextView textView = new TextView(this);
    textView.setId(integer);

    textView.setText(text);

    textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    mLayout.addView(textView,1);
    integer +=1;

} 
更新


如果您想添加多个文本视图,我建议您添加一个列表视图,然后在列表视图中添加文本视图。

发布您的xml和活动代码在EditText中创建下面的线性布局,然后创建运行时文本视图并添加此线性布局。这只是一个问题:android:layout\u weight=“1”无法工作,编辑文本将尽可能缩小。如何修复它?这不是我想要的,因为我希望新的文本视图显示在edittext下面…不,我希望edittext和按钮在一行上,而edittext下面的文本编辑在另一行上。@torque203的答案是可以的,只是现在edittext没有占用行中所有可能的空间。是的。你试过这个吗xml@Helios83您没有更改方向,这就是为什么在linearlayout中使用单线android:orientation=“vertical”,因此第一个linearlayout应具有垂直方向,第二个应具有水平方向,对吗?
private void createNewTextView(String text) {
    final TextView textView = new TextView(this);
    textView.setId(integer);

    textView.setText(text);

    textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    mLayout.addView(textView,1);
    integer +=1;

}