2016-10-10 11 views
0

私のアプリのボタンが押されるたびに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); 

} 

私はに実行している問題は、私はボタンをクリックしたときに、私は次のよう得ることです:

AppImage

は私がするのEditTextボックスを望みます私がxmlで定義したものと同じ高さですが、画面全体を占めています。

アイデア?上記のコードで

+0

remove * layout.setWeightSum(1f); params.weight = 1.0f; * – Atula

答えて

0
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
     LinearLayout.LayoutParams.WRAP_CONTENT); 

、あなたはmatch_parentに設定し、高さを持っており、wrap_contentに設定widht。代わりに、高さと幅をXMLに合わせて設定してみてください。高さは58dpで、幅は親と一致する必要があります。

希望すると便利です。

0

私はこれをテストする時間を持っていないが、あなたは試すことができます:あなたは、その後のパラメータを作成するのではなく、これらを使用することができます

// assuming you have variables for ingredient_list and edit_message 
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ingredientList.getLayoutParams(); 
LinearLayout.LayoutParams editTextParams = (LinearLayout.LayoutParams)editMessage.getLayoutParams(); 

P.S.あなたはxmlのあなたの原料リストに指定された方向性を持っていません。

1

Java内でレイアウトを完全に再定義する必要はありません。それを独自のXMLファイルとして定義する必要があります。電話ingredient_input.xml

たとえば、自由に変更できます。これはEditTextとボタンのための1つの "行"で、水平方向のレイアウトです。 RelativeLayoutでもかまいません。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="58dp" 
    android:weightSum="1" 
    android:orientation="horizontal" 
    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:id="@+id/btnAddMore" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/button_send"/> 
    </LinearLayout> 

まず、あなたが次に

LinearLayout ll = (LinearLayout) findViewById(R.id.ingredient_list); 

親線形のレイアウトを取得する必要があり、あなたは、「入力行」

LayoutInflater inflater = LayoutInflater.from(MainActivity.this); // some context 
View row = inflater.inflate(R.layout.ingredient_input, null); 

のためのXMLファイルを膨らませることができますも見つけることができますボタンをクリックし、クリックリスナーを設定します。

row.findViewById(R.id.btnAddMore).setOnClickListener(...); // TODO 

はその後、ちょうどあなたが本当に代わりに、アダプタとリストビューを使用することができ、

ll.addView(row); 

現実的に親線形のレイアウト上にそのビューを追加します。

関連する問題