我正在开发一个Android应用程序,我想在@id
下面添加EditText
,这是第一次添加到XML中,然后在代码中添加到另一个下面。即使我从前200名获得保证金,新的EditText也出现在右上角。我做错了什么?
XML文件:
EditText
Java代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:orientation="horizontal"
>
<RelativeLayout
android:id="@+id/menuLayout"
android:layout_width="wrap_content"
android:layout_height="1000dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="180dp"
android:background="@drawable/bottom_border"
android:padding="3dp"
android:scaleType="fitXY"
android:src="@drawable/mittag_top" />
<EditText
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="80dp"
android:layout_marginStart="80dp"
android:layout_marginTop="125dp"
android:hint="Menu karte datum..."
android:lineSpacingExtra="10dp"
android:lineSpacingMultiplier="1"
android:paddingTop="7dp"
android:textColor="@color/abc_primary_text_material_dark"
android:textColorHint="@color/abc_primary_text_material_dark"
android:textSize="12sp" />
<EditText
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignLeft="@+id/menuDate"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/menuDate"
android:layout_below="@+id/menuDate"
android:hint="Opening times..."
android:lineSpacingExtra="10dp"
android:lineSpacingMultiplier="1"
android:paddingTop="7dp"
android:textColor="@color/abc_primary_text_material_dark"
android:textColorHint="@color/abc_primary_text_material_dark"
android:textSize="12sp" />
<EditText
android:id="@+id/firstEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/openTime"
android:textColor="@color/abc_primary_text_material_dark"
android:textColorHint="@color/abc_primary_text_material_dark"
android:hint="Menu entry.." />
</RelativeLayout>
</ScrollView>
我做错了什么?如何在最后添加的EditText下添加EditText。谢谢。
更新
更新了addEdittext()
public class AddStuff extends Activity{
EditText firstEntry, date, time;
RelativeLayout relativeLayout;
Typeface type;
int counter = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ocr_menu);
type = Typeface.createFromAsset(getAssets(),"fonts/MyFont.ttf");
firstEntry = (EditText)findViewById(R.id.firstEntry);
firstEntry.setTypeface(type);
date = (EditText)findViewById(R.id.date);
date.setTypeface(type);
time = (EditText)findViewById(R.id.time);
time.setTypeface(type);
relativeLayout = (RelativeLayout)findViewById(R.id.menuLayout);
firstEntry.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
addEditText();
}
}
});
}
public void addEditText(){
EditText editText = new EditText(getApplicationContext());
editText.setHint("Entry number "+counter);
// editText.setPadding(2, 200, 2, 2);
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(2, 300, 0, 0);
editText.setHintTextColor(Color.WHITE);
editText.setTypeface(type);
relativeLayout.addView(editText);
counter++;
}
}
答案 0 :(得分:1)
因为在RelativeLayout
中添加动态EditText所以需要使用LayoutParams .addRule
而不是margin来对齐EditText:
EditText editText = new EditText(AddStuff.this);
...
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// get last child id from RelativeLayout
int idLastChild=relativeLayout.getChildAt(relativeLayout.getChildCount()-1);
params.addRule(RelativeLayout.BELOW,idLastChild);
// set id for EditText
editText.setId(idLastChild+1);
// set layout params
editText.setLayoutParams(params);
relativeLayout.addView(editText, params);
答案 1 :(得分:0)
这是一个相对布局。不是LinearLayout。您应指定视图的位置取决于其他视图的方式。
RelativeLayout.LayoutParams params
= (RelativeLayout.LayoutParams) yourView.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.firstEntry);
答案 2 :(得分:0)
您正在创建参数,但不会将它们分配给布局。请按以下步骤操作:
EditText editText = new EditText(getApplicationContext());
final LayoutParams lparams = new LayoutParams(50,30); // Width , height
editText.setLayoutParams(lparams);
答案 3 :(得分:0)
试试这个
public void addEditText(){
EditText editText = new EditText(getApplicationContext());
editText.setHint("Entry number "+counter);
// editText.setPadding(2, 200, 2, 2);
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(2, 300, 0, 0);
editText.setHintTextColor(Color.WHITE);
editText.setTypeface(type);
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
newParams.addRule(RelativeLayout.BELOW, R.id.date);
editText.setLayoutParams(newParams);
relativeLayout.addView(editText);
counter++;
}