0
我怎樣才能以編程方式添加新的EditText到RelativeLayout,這是在LinearLayout?Android的更新RelativeLayout這是在LinearLayout
我的佈局:
<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」。謝謝。
工作得很好!謝謝 :) – user2350530