私のアプリのボタンが押されるたびにEditText要素を含む新しいLinearLayoutを追加しようとしています。私が持っているXMLは、私はそれの内側のEditTextと一緒に第二のLinearLayoutを複製しようとしていますリニアレイアウトをプログラムで追加する
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/app"
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="vertical"
android:weightSum="1">
<EditText android:id="@+id/dish_name"
android:layout_width="match_parent"
android:layout_height="58dp"
android:hint="@string/dish_name"
android:background="@drawable/my_border"
android:paddingLeft="10dip"/>
<LinearLayout android:id="@+id/ingredient_list"
android:layout_width="match_parent"
android:layout_height="58dp"
android:weightSum="1"
android:layout_marginTop="20dp">
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="42dp"
android:hint="@string/edit_message"
android:background="@drawable/my_border"
android:layout_weight="1"
android:paddingLeft="10dip"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
</LinearLayout>
です。私がこれまで持っているJavaのコードは次のとおりです。
public void sendMessage(View view) {
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setWeightSum(1f);
params.weight = 1.0f;
params.setMargins(0,20,0,0);
EditText editText = new EditText(this);
editText.setBackgroundResource(R.drawable.my_border);
editText.setHint(R.string.edit_message);
editText.setLayoutParams(params);
LinearLayout container = (LinearLayout)findViewById(R.id.app);
container.addView(editText);
}
私はに実行している問題は、私はボタンをクリックしたときに、私は次のよう得ることです:
は私がするのEditTextボックスを望みます私がxmlで定義したものと同じ高さですが、画面全体を占めています。
アイデア?上記のコードで
remove * layout.setWeightSum(1f); params.weight = 1.0f; * – Atula