1
我想重复使用现有的布局,而不是创建/编写新的xml。目前,我喜欢这个。以编程方式膨胀以获得android布局
我想有这样的程序,而不是书面的XML(可能是我想将它写活动)。
我可以知道该怎么办?另外,另一个问题是,如果我这样重复使用,“id”将是相同的。如果是这样,我如何设置文本?
我想重复使用现有的布局,而不是创建/编写新的xml。目前,我喜欢这个。以编程方式膨胀以获得android布局
我想有这样的程序,而不是书面的XML(可能是我想将它写活动)。
我可以知道该怎么办?另外,另一个问题是,如果我这样重复使用,“id”将是相同的。如果是这样,我如何设置文本?
添加的LinearLayout你的布局,然后充气,代码多次header_description布局,并把它添加到的LinearLayout。
更改布局与此类似:
<include layout="@layout/header_list_image"/>
<LinearLayout
android:layout_id="@+id/description_layout"
android:layout_width="match_parent"
android:layout_height="wrap_contents"
android:orientation="vertical" >
<include layout="@layout/header_url"/>
<include layout="@layout/row_listing_like_comment_share"/>
<include layout="@layout/header_comment_separator"/>
在代码中,使用id找到的LinearLayout,以及膨胀的描述布局一个在时间和您的onCreate将它们添加()函数:
LinearLayout layout = (LinearLayout)findViewById(R.id.description_layout);
LayoutInflater inflater = getLayoutInflater();
// add description layouts:
for(int i = 0; i < count; i++)
{
// inflate the header description layout and add it to the linear layout:
View descriptionLayout = inflater.inflate(R.layout.header_description, null, false);
layout.addView(descriptionLayout);
// you can set text here too:
TextView textView = descriptionLayout.findViewById(R.id.text_view);
textView.setText("some text");
}
此外,另一个问题是,如果我再利用这样的,“ID”将是相同的。如果是这样,我如何设置文本?
呼叫findViewById()上膨胀布局e.g:
descriptionLayout.findViewById(R.id.text_view);
不喜欢这样的:
findViewById(R.id.text_view);