Das hinzufügen von Ansichten zum LinearLayout programmgesteuert
Habe ich eine Tätigkeit, die zeigt Kommentare. Die Kommentare selbst haben ein layout, also kann ich nicht einfach eine ListView.
Ich bin das hinzufügen der Kommentare mit einer Schleife, und das Programm geht Sie durch die ganze Schleife (geprüft via LogCat), sondern fügt nur die erste Ansicht (Kommentar) zu dem linearlayout.
Mein code (in der Realität sind die fillComments parameter wird etwas anderes sein als String[]):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_layout);
String[] comments = {"kommentaar 1", "kommentaar 2", "kommentaar 3"};
mTitle = (TextView) findViewById(R.id.comments_title);
mTextArea = (EditText) findViewById(R.id.comment_editor);
mAddButton = (Button) findViewById(R.id.add_comment);
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
mTitle.setText(getIntent().getStringExtra("name"));
fillComments(comments);
}
private void fillComments(String[] comments) {
View comment;
TextView commentator;
TextView commentDate;
TextView commentText;
LayoutInflater inflater = getLayoutInflater();
for (String s : comments) {
Log.d("Comment adder", "Adding comment " + s);
comment = inflater.inflate(R.layout.comment_row_layout, null);
commentator = (TextView) comment.findViewById(R.id.commentator);
commentDate = (TextView) comment.findViewById(R.id.comment_date);
commentText = (TextView) comment.findViewById(R.id.comment_text);
commentator.setText("Test commentator");
commentDate.setText("12-12-2012");
commentText.setText(s);
mCommentArea.addView(comment);
}
}
Es passiert auch, wenn
können Sie bitte zeigen Sie uns, layout: comment_row_layout.xml.. ich vermute du hast die horizontale Ausrichtung...
R.id.comments_area
hat vertikale Ausrichtung + Breite fill_parent ? Und ist die log ("Bemerkung " adder") mehrfach angezeigt ?können Sie bitte zeigen Sie uns, layout: comment_row_layout.xml.. ich vermute du hast die horizontale Ausrichtung...
InformationsquelleAutor j0ntech | 2012-01-26
Du musst angemeldet sein, um einen Kommentar abzugeben.
denke ich
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
dieses layout die Ausrichtung ist Horizontal, so dass dieser Probleme Auftritt. bitte, wenn einer horizontalen Ausrichtung dann ändern Sie es zu vertikalen und genießen Sie
InformationsquelleAutor Ramesh Solanki
Wie haben Sie das LinearLayout? Es könnte nur ein Problem angezeigt.
Überprüfen Sie die Größe und Ausrichtung des LinearLayout.
android:orientation="vertical"
Linie zu meinem LinearLayout.InformationsquelleAutor user1169390