2016-08-01 6 views
1

私はアンドロイドプログラミングの新人です。プロジェクトをやっているうちに問題が発生しました。私はテキストとボタンを含む線形レイアウトの数を追加する必要があります。私はコードを書いたが、それは単に前のものに1つのレイアウトを置くだけである。レイアウトをリストに入れるために、コード内で何を変更する必要がありますか。アクティビティに複数のレイアウトを追加するには

package com.example.sanzharaubakir.fin; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by sanzharaubakir on 01.08.16. 
*/ 
public class Scanned extends AppCompatActivity { 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.scanned); 
     Intent in = getIntent(); 
     int num = in.getIntExtra("n", 0); 
     String [] s = in.getStringArrayExtra("arr"); 
     for (int i = 0 ; i < num; i++) 
      Toast.makeText(getApplicationContext(), "" + s[i], Toast.LENGTH_LONG).show(); 
     List<String> str = new ArrayList<String>(); 
     for(int i = 0; i < num; i++) 
     { 
      str.add(s[i]); 
     } 
     final List<Integer> intList = new ArrayList<Integer>(); 
     for (int i = 0 ;i < num; i++) 
     { 
      final LinearLayout l = new LinearLayout(this); 
      l.setOrientation(LinearLayout.HORIZONTAL); 
      LinearLayout.LayoutParams lParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
      setContentView(l, lParam); 
      ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

      TextView v = new TextView(this); 
      v.setText(s[i]); 
      v.setLayoutParams(lpView); 
      l.addView(v); 

      Button delete = new Button(this); 
      delete.setText("Delete"); 
      delete.setLayoutParams(lpView); 
      l.addView(delete); 

      final int finalI = i; 
      delete.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        intList.add(finalI); 
        l.setVisibility(View.GONE); 
       } 
      }); 
     } 

    } 
} 

とXMLファイルだけで、次のようになります:ここで

は、コードがある

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</LinearLayout> 
+0

あなたが誠実な努力をするなら、誰かがすでに同様の質問をしている可能性があり、答えを受け取った可能性があります。この質問を見て:http://stackoverflow.com/questions/12077928/android-i-need-to-arrange-the-layout-of-my-application –

+0

@steady_daddyこれは私が探しているものではない、しかしありがとうございます – Sanzhar

+0

私は確信していませんが、親にマッチするように設定されている線幅のレイアウトの幅と高さを設定することは重複を引き起こしていると思いませんか? –

答えて

0

そのをのLinearLayoutインスタンスを作成しないでくださいsetContentViewを使用しているため、アクティビティレイアウトは新しく作成されたレイアウトとして設定されます時間。あなたが望むことをするには、R.layout.scanned xmlファイルのIDをあなたのlinearlayoutに設定し、他の線形レイアウトをこれに加えてください。また、上記のループからsetContentViewを削除してください。あなたが何か問題に直面したら教えてください。

0

は、forloop内

final List<Integer> intList = new ArrayList<Integer>(); 
final LinearLayout l = new LinearLayout(this); 
     l.setOrientation(LinearLayout.HORIZONTAL); 
     LinearLayout.LayoutParams lParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     setContentView(l, lParam); 
    for (int i = 0 ;i < num; i++) 
    { 

     ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

     TextView v = new TextView(this); 
     v.setText(s[i]); 
     v.setLayoutParams(lpView); 
     l.addView(v); 

     Button delete = new Button(this); 
     delete.setText("Delete"); 
     delete.setLayoutParams(lpView); 
     l.addView(delete); 

     final int finalI = i; 
     delete.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       intList.add(finalI); 
       l.setVisibility(View.GONE); 
      } 
     }); 
    } 
+0

ありがとうございますが、私はまだこの問題を抱えています。レイアウトは別のものになります – Sanzhar

+0

私は自分のコードを試しましたが、{textview1、button1} {textview2、button2}のように、レイアウトをぼろぼろにしています。 – prakash

0

以下のコードを使用してください。

final List<Integer> intList = new ArrayList<Integer>(); 
    final LinearLayout l = new LinearLayout(this); 
    LinearLayout.LayoutParams lParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    for (int i = 0; i < num; i++) { 

     l.setOrientation(LinearLayout.HORIZONTAL); 
     l.setLayoutParams(lParam); 
     //setContentView(l, lParam); 
     ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

     TextView v = new TextView(this); 
     //v.setText(s[i]); 
     v.setLayoutParams(lpView); 
     l.addView(v); 

     Button delete = new Button(this); 
     delete.setText("Delete"); 
     delete.setLayoutParams(lpView); 
     l.addView(delete); 

     final int finalI = i; 
     delete.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       intList.add(finalI); 
       l.setVisibility(View.GONE); 
      } 
     }); 
    } 
    setContentView(l,lParam); 

私のために働いてくれます!!

関連する問題