java通过循环以编程方式添加视图,第一个元素不显示
我循环浏览一组对象,并为每个对象的水平滚动视图添加一个带有对象名称的自定义视图。我的问题是,第一个总是消失
以下是我的活动布局:
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="lp.german.bischofshofpresenter.app.PraesentationsActivity">
<TextView
安卓:text="Hier wird das pdf gezeigt"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:gravity="center"/>
<HorizontalScrollView
安卓:layout_width="fill_parent"
安卓:layout_height="200dp"
安卓:layout_alignParentBottom="true"
安卓:id="@+id/file_scroll_view"
>
<LinearLayout
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:id="@+id/file_scroll_view_linear_layout"
安卓:orientation="horizontal"
安卓:layout_gravity="center">
</LinearLayout>
</HorizontalScrollView>
我用以下代码填充线性布局:
for(int i = 0; i<files.length; i++)
{
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.file_template, null);
TextView textView = (TextView)v.findViewById(R.id.file_template_text);
textView.setText(files[i].Name());
Log.v("FILENAME:",files[i].Name());
View fileList = findViewById(R.id.file_scroll_view_linear_layout);
((ViewGroup)fileList).addView(v, ((ViewGroup)fileList).getChildCount());
}
我得到以下结果:
第一个对象名是File0,所以缺少这个。 真正奇怪的是,右边有一个与我的一个元素大小完全匹配的空白
请看这里:
我已经检查了日志输出,它循环了所有10个元素,并在那里运行,所以我可以 我无法解释那种行为
可能是我在((ViewGroup)fileList).addView(v, ((ViewGroup)fileList).getChildCount());
中设置的位置有问题,但我不知道这里出了什么问题
谢谢转发;)
共 (0) 个答案