【问题标题】:Creating multiple instances of xml android layout创建多个xml android布局实例
【发布时间】:2016-08-17 21:34:14
【问题描述】:

希望你一切安好,

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

我有以下函数,它接收一个数组列表,如下所示:

  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 
}

这可能吗? 有什么帮助吗?!

问候..

【问题讨论】:

  • 不确定您要做什么,但请查看带有自定义适配器的 ListView

标签: android xml layout arraylist


【解决方案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+", ""));
    }
}

然后您可以在 Activity 中使用此视图,例如:

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:

ViewGroup v = (ViewGroup)findViewById(...);
v.addChild(layouts[0]);
layouts[0].drawStudent(students.get(i)); 

希望这有帮助

注意:此代码只是一个示例,但未经测试也不保证,这只是如何做的想法

更新 - 直接使用 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;

}

【讨论】:

  • 谢谢你,亲爱的,我无法理解清楚,我所需要的只是为每个学生创建一个新的布局,如 xml 文件和一个新的 textview 和 imageview,然后将它们存储在 layouts 数组中。 .所有这些都需要在for循环中完成..我不明白你的代码的概念..
  • 不客气,很抱歉,如果我的回答不清楚,我的意思是,如果您使用代表您的 xml 布局的 CustomView ... 如果您想使用 xml直接地,您可能需要每次在循环中膨胀 xml 文件并将其分配给循环中的 RelativeLayout,并访问它的子项以设置它们(TextView 和 ImageView),然后将此 RelativeLayout 添加到将显示它们的 ViewGroup 中。 . 但不确定这一点
  • 再次感谢,也许第二个想法就是我的意思,但是如何做到这一点?!任何参考或样本?!
  • 不客气,我更新了答案,但请注意我没有运行代码来测试它,这只是想法
猜你喜欢
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多