我有以下XML布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical"
android:weightSum="1"
tools:showIn="@layout/activity_main">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<TextView
android:layout_width="match_parent"
android:layout_height="64dp"
android:text=""
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/progressBar1" />
<ImageView
android:layout_width="match_parent"
android:layout_height="406dp"
android:id="@+id/img"
android:layout_below="@+id/textView" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerHorizontal="true" />
</RelativeLayout>
简单地说,它包含Textview和Image视图。
我有以下功能接收arraylist如下:
public void DisplyOnTextView(List< Student > students) {
textView.setText(students.get(i).getDes());
new LoadImage().execute(students.get(i).getUrl().replaceAll("\\s+", ""));
}
我想要做的是循环接收(学生)数组列表以创建xml布局的新实例,该实例将包含数组中每个学生的textview和imageview,然后将每个布局存储在数组中,如下所示:
int[] layouts = {R.layout.instance1, R.layout.instance2,...};
我的循环看起来像:
for(int i=0;i<students.size(); i++){
// create new layout for student[i]
// create new textview for student[i]
//create new imageview for student[i]
// layouts[i] = the new created layout
}
这可能吗?
有什么帮助吗?!
问候..
答案 0 :(得分:3)
您可以声明自定义视图,并使其扩展RelativeLayout,并在其构建器中调用
...
printTemp(input.nextDouble());
然后您可以实例化自定义视图并将其作为子视图放在任何其他ViewGroup
中您甚至可以在此自定义视图中执行操作,例如制作显示数据的方法...例如
inflate(getContext(), R.layout.your_xml, this);
然后您可以在Activity中使用此视图,如:
public class CustomView extends RelativeLayout{
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private TextView tv;
private ImageView iv;
private void init(){
inflate(getContext(), R.layout.your_xml, this);
tv = findViewById(R.id.textView);
iv = findViewById((R.id.textView); // your imageview id is textview !!?
}
public void drawStudent(Student student){
textView.setText(student.getDes());
new LoadImage().execute(student.getUrl().replaceAll("\\s+", ""));
}
}
然后,您可以通过以下方式将这些视图添加到任何ViewGroup:
CustomView[] layouts = new CustomView[students.size()];
for(int i=0;i<students.size(); i++){
// create new layout for student[i]
CustomView v = new CustomView(this);
// layouts[i] = the new created layout
layouts[i] = v;
}
希望这有帮助
注意:此代码只是一个示例,但它没有经过测试,也没有保证,只是如何做到的想法
更新 - 直接使用XML:
循环中的可以执行以下操作:
ViewGroup v = (ViewGroup)findViewById(...);
v.addChild(layouts[0]);
layouts[0].drawStudent(students.get(i));