【发布时间】:2014-11-16 17:57:18
【问题描述】:
在我的应用程序中,我有简单的布局,可以填充任何具有类结构的数据。添加项目后,我想将此布局添加到滚动视图中。将布局添加到滚动视图是成功的,但我无法将数据设置为自定义布局。例如,这是我设置数据的自定义布局:
tile.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/drop_shadow">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="30dp" android:minHeight="100dp" android:background="#eee">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="@+id/username"/>
</LinearLayout>
</LinearLayout>
类数据结构:
public class SubjectStructure {
public String username;
public String topic;
public String description;
public String avatar;
public String sender;
public Integer grade;
public Integer type;
public String date;
}
现在我想填充 tile.xml 并将其添加到下面的布局中:
fragment_home.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/scrollView1">
</LinearLayout>
</ScrollView>
我的代码是这样的: public View onCreateView(LayoutInflater inflater, ViewGroup 容器, 捆绑 savedInstanceState) {
View rootView = inflater.inflate( R.layout.fragment_home, container, false);
scrollView = (LinearLayout) rootView.findViewById ( R.id.scrollView1 );
for(SubjectStructure SS: G.subject_items){
LinearLayout newLL = new LinearLayout( G.context );
TextView newTV = new TextView( G.context );
newTV.setText ( SS.topic );
newLL.addView(newTV);
LayoutInflater in = (LayoutInflater) G.context.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.tile, null);
scrollView.addView(view);
}
return rootView;
}
在此代码中,我可以将 tile.xml 添加到 fragment_home.xml 。但我不能将文本SS.topic 设置为tile.xml。 SS.topic 不为空并且有数据。如何解决这个问题?
【问题讨论】:
标签: android xml android-layout