如何以编程方式将新的 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”。谢谢。