0

如何以编程方式将新的 EditText 添加到 LinearLayout 中的 RelativeLayout?

我的布局:

    <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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/bg"
    android:id="@+id/mainlayout"
    tools:context=".NwActivity" android:baselineAligned="false">

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

        <TextView 
            android:id="@+id/teamA"
            android:text="@string/teamA"
            android:textSize="24sp"
            style="@style/Txt" />

        <Button
            android:id="@+id/pAddA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/teamA"
            style="@style/Txt"
            android:text="@string/pAdd"
            android:onClick="addA" />

    </RelativeLayout>

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

        <TextView 
            android:id="@+id/teamB"
            android:text="@string/teamB"
            android:textSize="24sp"
            style="@style/Txt" />

        <Button
            android:id="@+id/pAddB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/teamB"
            style="@style/Txt"
            android:text="@string/pAdd"
            android:onClick="addB" />

    </RelativeLayout>

</LinearLayout>

我的活动:

    package basketball.counter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

public class NwActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nw);
    }

    public void addA(View view) {
        LinearLayout ll = (LinearLayout) findViewById(R.id.mainlayout);
        EditText et = new EditText(this);
        et.setHint(R.string.pHint);
        ll.addView(et);
        setContentView(ll);
    }

    public void addB(View view) {

    }

}

我只在 mainLayout 上得到它。我也需要设置 EditText 参数“LayoutBelow”。谢谢。

4

1 回答 1

1

在运行时添加的整个edittext中尝试此操作,您可以通过xml通过提供id来添加它

      RelativeLayout rl=(RelativeLayout)findViewById(R.id.relativelayoutid);      

    Edit Text et=new EditText(this);
    et.setHeight(50);
    et.setWidth(50);
    et.setId(1);



     EditText et1=new EditText(this);
    et1.setHeight(50);
    et1.setWidth(50);
    et1.setId(3);

          RelativeLayout.LayoutParams params1= new RelativeLayout.LayoutParams
        ( LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);



        params1.addRule(RelativeLayout.BELOW,et.getId());
        params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        et1.setLayoutParams(params1);
            rl.addView(et1);

希望对你有帮助

于 2013-05-30T07:10:33.740 回答