2016-04-24 156 views
0

的多个实例,希望你很好,很好,创建XML的Android布局

我有以下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和图像视图。

,我有如下其接收一个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 
} 

这是可能的吗? 有帮助吗?!

问候..

+1

不知道你正在尝试做的,但看看'ListView'定制适配器 – Yazan

回答

1

你可以宣布你的自定义视图,并使其扩展的RelativeLayout,并在它的构造函数,你会调用

inflate(getContext(), R.layout.your_xml, this); 

那么你可以实例化自定义视图,并把它作为孩子任何其他的ViewGroup

,你甚至可以做你的东西,在这个自定义视图,喜欢拍的方法来显示您的数据......例如

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 v = (ViewGroup)findViewById(...); 
v.addChild(layouts[0]); 
layouts[0].drawStudent(students.get(i)); 

希望这有助于

注:

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; 

} 

,那么你可以通过添加这些意见,以任何的ViewGroup这代码只是一个示例,但它没有经过测试,也没有保证,它只是如何做到这一点的想法

更新 - 使用直接XML:

在你的循环,你可以做如下:

ViewGroup[] layouts = new ViewGroup[students.size()]; 

LayoutInflater i = LayoutInflater.from(getApplicationContext());  
for(int i=0;i<students.size(); i++){ 
// create new layout for student[i] 
ViewGroup v = i.inflate(R.layout.your_xml,null); // you can use RelativeLayout instead of ViewGroup since your root view is RelativeLayout 

// setup textView 
TextView tv = (TextView) v.getChildAt(1) // child view at index 1 
tv.setText(...); 

// setup imageView 
ImageView iv = (ImageView) v.getChildAt(2) // child view at index 2 
new ImageLoader..... 

// layouts[i] = the new created layout 
layouts[i] = v; 

} 
+1

谢谢亲爱的,我无法清楚地理解它,所有我需要的是创建一个新的布局,如xml文件和每个学生的新textview和imageview,然后将它们存储在布局数组内..所有这些都需要在for循环..我不明白你的代码的概念.. – MSMC

+0

欢迎您,如果我的答案不明确,我的意思是说,如果您使用代表您的XML布局的CustomView是可能的。 ..如果你想使用xml directt,你可能需要在循环中每次膨胀xml文件,并将它分配给循环中的RelativeLayout,并访问它的子集来设置它们(TextView和ImageView),然后将此RelativeLayout添加到ViewGroup这将显示他们......但不知道这个 –

+0

再次感谢,也许第二个想法是我的意思,但如何做到这一点?任何参考或样本?! – MSMC