2016-07-29 2 views
0

いくつかのLinearLayoutsを挿入したいが、そうする必要はない。それは1つだけを挿入しますが、それ以上挿入する必要があります。異なる値のレイアウトを挿入する

LinearLayout commentsContainer = (LinearLayout) findViewById(R.id.view_comment_container); 
        commentsContainer.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
        commentsContainer.setOrientation(LinearLayout.HORIZONTAL); 
        for (int i = 0; i < postView.commentLenght(); i++) { 
         Log.e("LENGTH", postView.commentLenght()+"x"+i); 
         LinearLayout commentContainer = new LinearLayout(PostViewActivity.this); 
         commentContainer.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
         LinearLayout userContainer = new LinearLayout(PostViewActivity.this); 
         userContainer.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
         userContainer.setOrientation(LinearLayout.VERTICAL); 
         commentContainer.setOrientation(LinearLayout.HORIZONTAL); 
         commentContainer.setPadding(25,0,0,0); 
         ImageView commentImage = new ImageView(PostViewActivity.this); 
         commentImage.setLayoutParams(new LinearLayout.LayoutParams((int) ((float) width/6), (int) ((float) width/6))); 
         commentImage.setImageBitmap(postView.getComment(i).getImage()); 
         TextView commentText = new TextView(PostViewActivity.this); 
         commentText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
         commentText.setText(postView.getComment(i).getText()); 
         TextView displayUserText = new TextView(PostViewActivity.this); 
         displayUserText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
         displayUserText.setText(postView.getComment(i).getDisplayName()); 
         Log.d("TEXT", postView.getComment(i).getText()); 
         Log.e("TEXT", displayUserText.getText()+""); 
         displayUserText.setTag(postView.getComment(i).getUsername()); 
         displayUserText.setTextSize(12); 
         displayUserText.setTextColor(getResources().getColor(R.color.colorAccent)); 
         userContainer.addView(commentImage); 
         userContainer.addView(displayUserText); 
         commentContainer.addView(userContainer); 
         commentContainer.addView(commentText); 
         commentsContainer.addView(commentContainer); 

        } 

もう1つの奇妙なこと:最初のLog.dは常に正しいものですが、2番目のLog.dは常に同じです。間違いは何ですか?

答えて

1

このコードは、XMLリソースファイルを使用して大幅にクリーンアップすることができます。コメントのそれぞれが同じ形式をとるので、同じレイアウトをそれぞれに使用することができます。必要に応じてレイアウトに異なる値を入力することができます。これらはすべて、別のレイアウト内のViewGroupに追加できます。

レイアウトをプログラムで生成すると、XMLレイアウトを使用するよりもエラーが発生しやすく複雑になります。

Androidの公式ドキュメントにいくつかの読書をしていることは絶対に価値がある! https://developer.android.com/guide/topics/ui/declaring-layout.html

+0

ありがとう!はい、本当に良いです:-) – user6586661

関連する問題