【问题标题】:Programmatically create multiple LinearLayouts以编程方式创建多个 LinearLayout
【发布时间】:2012-11-23 18:50:43
【问题描述】:

我正在尝试创建一个线性布局的多个实例。我创建了我的布局文件(下)。我把它放在它自己的布局文件中,从另一个文件(main_activity.xml)我想在滚动视图中添加动态数量的线性布局实例。我知道我可以在我的 java 代码中以编程方式创建相同的线性布局并循环遍历它,但我宁愿不这样做(主要有两个原因)

  1. 在我的 java 类中创建大量布局开始让我的代码看起来很混乱 :)。
  2. 我喜欢 XML 布局和 Java 逻辑的范例。

我是否可以将我的逻辑保留在 java 中,然后动态创建/实例化我的线性布局的多个“实例”。我的猜测是有一个简单的解决方案,但我的新手状态正在影响我解决它的能力。出于某种原因,我的 java 代码可以找到 LinearLayout 但无法找到第一个 TextView(@+id/submittedBy)。

非常感谢, 克雷格

Java 逻辑

  ScrollView sv = (ScrollView) findViewById(R.id.existingCommentsScrollView);
    //DEBUGGING LOOP- REMOVE ME
    for(int i=0;i<30;i++){
        LinearLayout ll = (LinearLayout)findViewById(R.id.existingCommentLinearLayout);

        TextView submittedByTextView = (TextView) findViewById(R.id.submittedBy);
        submittedByTextView.setText("Craig " +i);

        TextView submittedDate = (TextView) findViewById(R.id.dateField);
        submittedDate.setText("SubmittedDate " +i);

        RatingBar showRating = (RatingBar) findViewById(R.id.userShowRating);
        showRating.setRating(3);

        RatingBar soundRating = (RatingBar) findViewById(R.id.userSoundRating);
        soundRating.setRating(1);

        EditText commentText = (EditText) findViewById(R.id.userCommentEditText);
        commentText.setText("THIS IS WHERE MY TEXT GOES FOR "+i+" I could add more too");

        sv.addView(ll);
    }
    sv.refreshDrawableState();

布局:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/linear_layout_border"
android:orientation="vertical"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="4dp"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

    <TextView
        android:id="@+id/submittedBy"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal"
        android:inputType="none"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:paddingTop="1dp"
        android:text="NameGoesHere"
        android:textColor="#E6E6E6"
        android:textSize="8sp" />

    <TextView
        android:id="@+id/dateField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/submittedBy"
        android:ems="10"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:paddingTop="1dp"
        android:text="DateGoesHere"
        android:textColor="#E6E6E6"
        android:textSize="8sp" />
</RelativeLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="4dp"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

    <TextView
        android:id="@+id/userShowRatingTextView"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal"
        android:inputType="none"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:paddingTop="1dp"
        android:text="Show Rating"
        android:textColor="#E6E6E6"
        android:textSize="8sp" />

    <RatingBar
        android:id="@+id/userShowRating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/userShowRatingTextView" />
</RelativeLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="4dp"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

    <TextView
        android:id="@+id/userSoundRatingTextView"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toRightOf="@id/userShowRating"
        android:gravity="center_horizontal"
        android:inputType="none"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:paddingTop="1dp"
        android:text="Sound Rating"
        android:textColor="#E6E6E6"
        android:textSize="8sp" />

    <RatingBar
        android:id="@+id/userSoundRating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/userSoundRatingTextView" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:inputType="none"
        android:paddingBottom="4dp"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:paddingTop="4dp"
        android:text="Comment Goes Here"
        android:textColor="#E6E6E6"
        android:textSize="12sp"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

        <requestFocus />
    </EditText>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout textview android-linearlayout


    【解决方案1】:

    你没有遇到任何异常吗?我指的是循环中的最后一行,在每次迭代中将LinearLayout 添加到ScrollView。您可能正在尝试扩充您的布局文件,然后将其添加到 ScrollView,如下所示:

    ScrollView sv = (ScrollView) findViewById(R.id.existingCommentsScrollView);
    // I assume this LinearLayout is the only child of the ScrollView above
    LinearLayout ll = (LinearLayout)findViewById(R.id.existingCommentLinearLayout);
    for(int i=0;i<30;i++){        
        // R.layout.the_layout_file is your layout from the question
        View inflatedView = getLayoutInflater().inflate(R.layout.the_layout_file, ll, false);
        TextView submittedByTextView = (TextView) inflatedView.findViewById(R.id.submittedBy);
        submittedByTextView.setText("Craig " +i);
    
        TextView submittedDate = (TextView) inflatedView.findViewById(R.id.dateField);
        submittedDate.setText("SubmittedDate " +i);
    
        RatingBar showRating = (RatingBar) inflatedView.findViewById(R.id.userShowRating);
        showRating.setRating(3);
    
        RatingBar soundRating = (RatingBar) inflatedView.findViewById(R.id.userSoundRating);
        soundRating.setRating(1);
    
        EditText commentText = (EditText) inflatedView.findViewById(R.id.userCommentEditText);
        commentText.setText("THIS IS WHERE MY TEXT GOES FOR "+i+" I could add more too");
    
        ll.addView(inflatedView);
    }
    

    另外,请记住,您要添加大量视图,这是为了获得流畅的界面而应避免的。

    【讨论】:

    • 你成功了!我没有掌握充气功能的作用。非常感谢。我同意这是很多观点,我也很担心。我仍在学习如何最小化所需的视图并保持强大的用户界面。再次感谢。
    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    相关资源
    最近更新 更多