Android addView()不显示视图
这应该很简单,但这个按钮不显示。我检查了logs-tags.size=5,所以问题不在这里。ll是一个线性布局 以下是布局文件:Android addView()不显示视图,android,view,Android,View,这应该很简单,但这个按钮不显示。我检查了logs-tags.size=5,所以问题不在这里。ll是一个线性布局 以下是布局文件: private void showText(){ textView.setText(Html.fromHtml(text)); Log.d("MyLog","tags.size="+tags.size()); for (int i=0;i<tags.size();i++){ Button button = new Button(context);
private void showText(){
textView.setText(Html.fromHtml(text));
Log.d("MyLog","tags.size="+tags.size());
for (int i=0;i<tags.size();i++){
Button button = new Button(context);
button.setText(tags.get(i));
ll.addView(button, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tagButtons.add(button);
}
}
因此,这是一个非常简单的代码,我不明白为什么这些视图没有添加到id为ll的布局中。您应该在父布局上调用invalidate,强制在屏幕上绘制子视图。请删除您在问题中未发布的linearlayout参考,并像我所做的那样使用,试试这个
如果您正在使用活动,请在OnCreate-method中调用showText
给定textview width=wrap\u content。给定scrollview width=match\u parent和height=match\u parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_margin="5dp"
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:orientation="horizontal"
android:id="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
<LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
private void showText(){
textView.setText(Html.fromHtml(text));
for (int i=0;i<tags.size();i++){
Button button = new Button(context);
button.setText(tags.get(i));
LinearLayout ll=(LinearLayout) findViewById(R.id.ll);
ll.addView(button);
}
}