【问题标题】:Android add data to custom layout and scrollviewAndroid将数据添加到自定义布局和滚动视图
【发布时间】: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.xmlSS.topic 不为空并且有数据。如何解决这个问题?

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    你肯定缺少的是

     scrollView.addView(newLL); 
    

    每次迭代。关于

    LayoutInflater in =  (LayoutInflater) G.context.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.tile, null);
     scrollView.addView(view);
    

    我不确定它的用途。您在每次迭代中创建一个 tile 的新实例,但您对其内容不做任何操作。在次要方面,您已经有一个 LayoutInflater。您不需要在每次迭代时向系统询问一个,使用您获得的一个作为参数。

    【讨论】:

      【解决方案2】:

      修改代码如下:

      for(SubjectStructure SS: G.subject_items){
          LayoutInflater in =  (LayoutInflater) G.context.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
          View view = inflater.inflate(R.layout.tile, null);
      
          TextView textUserName=(TextView) view.findViewById(R.id.username);
          textUserName.setText(SS.topic);
      
          scrollView.addView(view);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多