如何在循环中添加我的布局设计

时间:2016-08-23 04:24:23

标签: android

我正在尝试使用循环在一个根布局中添加多个布局,但我不知道如何做到这一点。我的布局是代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/framlayout"
tools:context="layout.SettingFragment">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/linearLayout"
        android:padding="10dp"
        android:weightSum="10">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="5dp"
            android:id="@+id/linearLayout1"
            android:background="@drawable/texview_design"
            android:backgroundTint="@color/popup_tv_color"
            android:layout_weight="1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Baatein Ye Kabhi Na"
                android:gravity="center"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="20dp"
                android:textStyle="italic"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hindi Romantic"
                android:paddingBottom="10dp"
                android:layout_gravity="center_horizontal"/>
            <RatingBar
                android:layout_width="144dp"
                android:layout_height="wrap_content"
                android:id="@+id/ratingBar1"
                android:layout_gravity="right" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

This但我想要I want this layout using loop

2 个答案:

答案 0 :(得分:1)

自定义列表视图是您的最佳选择 点击此链接:

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html

您是否希望通过添加以下代码来确保包含布局

    RelativeLayout rl = (RelativeLayout) findById(R.id.rl); //id of layout which layout you want add chields                
     LayoutInflater layoutInflater = (LayoutInflater) 
    this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
    rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) ); //content_layout is your layout with tw0 textview and rating bar

希望它适合你

答案 1 :(得分:0)

最好使用易于实现和构建此类任务的listview

但你可以添加如下

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0; i < 3; i++) {

            View view = inflater.inflate(R.layout.yourLayoutId, null);
            // Assume you want to add your view in linear layout
            linearLayout.addView(view);

        }