2016-04-24 153 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