Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一个不工作的Android系统中,在另一个文本视图的右侧膨胀文本视图_Java_Android_Android Layout_Arraylist - Fatal编程技术网

Java 在一个不工作的Android系统中,在另一个文本视图的右侧膨胀文本视图

Java 在一个不工作的Android系统中,在另一个文本视图的右侧膨胀文本视图,java,android,android-layout,arraylist,Java,Android,Android Layout,Arraylist,我试图在一个版面中膨胀一个版面,并在其中添加几个文本视图。为此,我为每个textview设置了一个标题的arrayList,并且设置了一个与arrayList大小相同的textview数组。问题是,这段代码只会膨胀一个TextView,而不是arrayList中的最后一个 public void inflatePeriodGradeRightOf() { AcademicStatusController.getInstance(this); Log.d(TAG, "type="

我试图在一个版面中膨胀一个版面,并在其中添加几个文本视图。为此,我为每个textview设置了一个标题的arrayList,并且设置了一个与arrayList大小相同的textview数组。问题是,这段代码只会膨胀一个TextView,而不是arrayList中的最后一个

 public void inflatePeriodGradeRightOf() {
    AcademicStatusController.getInstance(this);
    Log.d(TAG, "type="+instanceType+" id= "+instanceId);
    ArrayList<Item> periodsInfo = AcademicStatusController
            .getPeriodsInfo(instanceType, instanceId);

    RelativeLayout llDisplayData = (RelativeLayout) findViewById(R.id.help_relative_layout_for_periods);

    // Params to add some new rules
     RelativeLayout.LayoutParams relativeLayoutParams;
     relativeLayoutParams = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT,
     RelativeLayout.LayoutParams.WRAP_CONTENT);
     TextView tvtxt;
     TextView[] textViews = new TextView[periodsInfo.size()];
     int i = 0;

    for (Item info : periodsInfo) {
        // Inflate the view of the period
        View customView = inflater.inflate(
                R.layout.activity_academic_status_period_layout, null);
        textViews[i] = new TextView(this);

        // Add the first textView and then add the others right of the last
        // one
        PeriodItem item = (PeriodItem) info;
        if (periodsInfo.get(0) == info) {
            // Set the text to the TextView.
            textViews[i].setText(item.getName());
            textViews[i].setId(i+1);
            llDisplayData.addView(customView);
        } else {
            // Set the text to the TextView.
            textViews[i].setText(item.getName())
            textViews[i].setId(i+1);
            relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textViews[i-1].getId());
                textViews[i].setLayoutParams(relativeLayoutParams);
            llDisplayData.addView(customView, relativeLayoutParams);
        }

        i++;
    }
}
我要添加文本视图的主布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/carla123456"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/academic_status_banner"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:scaleType="fitXY"
            android:src="@drawable/banner_team" />

        <TextView
            android:id="@+id/academic_status_average"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="50dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/rounded_corner_blue"
            android:paddingBottom="5dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="5dp"
            android:text="Promedio: ND"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFF" />

        <TextView
            android:id="@+id/academic_status_subjects"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/academic_status_banner"
            android:background="@color/akdemia_blue"
            android:gravity="center"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:text="Materias"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFF" />

        <ListView
            android:id="@+id/academic_status_subjects_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/academic_status_subjects" >
        </ListView>

        <RelativeLayout
            android:id="@+id/help_relative_layout_for_periods"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:layout_centerHorizontal="true" >

        </RelativeLayout>
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <!-- should not be larger than 320 to show content -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>
我想要达到的是这样的效果:

但我只能让它膨胀arrayList中的最后一个元素


希望有人能帮我,提前谢谢

我发现了问题。我试图放大每个文本视图,而不是每个相对视图

我有一个视图数组和一个文本视图数组,我膨胀循环外的第一个布局以获得一个起点。然后,对于每个布局和文本视图,膨胀视图并找到文本视图,设置文本视图的文本,在我的例子中是periodsInfo ArrayList中的周期名称,为视图和文本视图生成ID,设置参数并添加具有此类参数的视图

我犯的一个错误是对每个布局使用相同的LayoutParams实例,这不起作用!您需要为要添加的每个视图创建LayoutParams的新实例

这是膨胀另一个相对人的相对人权利的代码

@SuppressLint("InflateParams")
    public void inflatePeriodGrade() {
        AcademicStatusController.getInstance(this);
        ArrayList<Item> periodsInfo = AcademicStatusController.getPeriodsInfo(
                instanceType, instanceId);

        // Find RelativeLayout where the period grade will be placed
        RelativeLayout llDisplayData = (RelativeLayout) findViewById(R.id.help_linear_layout_for_periods);

        View[] customViews = new View[periodsInfo.size()];
        TextView[] textViews = new TextView[periodsInfo.size()];
        //
        // Inflate the view of the period
        customViews[0] = inflater.inflate(
                R.layout.activity_academic_status_period_layout, null);
        textViews[0] = (TextView) customViews[0]
                .findViewById(R.id.academic_status_grade);

        // Set the text to the TextView.
        textViews[0].setText(((PeriodItem) (periodsInfo.get(0))).getName());
        customViews[0].setId(View.generateViewId());
        textViews[0].setId(View.generateViewId());
        llDisplayData.addView(customViews[0]);

        int i = 1;
        for (Item item : periodsInfo) {

            PeriodItem period = (PeriodItem) item;

            if (periodsInfo.get(0) == period) {
                continue;
            }

            customViews[i] = inflater.inflate(
                    R.layout.activity_academic_status_period_layout, null);
            textViews[i] = (TextView) customViews[i]
                    .findViewById(R.id.academic_status_grade);
            textViews[i].setText(period.getName());
            RelativeLayout.LayoutParams newParams;
            newParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            newParams.addRule(RelativeLayout.RIGHT_OF,
                    customViews[i - 1].getId());
            customViews[i].setId(View.generateViewId());
            textViews[i].setId(View.generateViewId());
            customViews[i].setLayoutParams(newParams);
            llDisplayData.addView(customViews[i], newParams);
            i++;
        }

    }

也许有一种更有效的方法可以做到这一点,但我就是这样做的。我真的希望这段代码能帮助一些人:

这是因为Id总是相同的,所以您将文本视图添加到其他视图的顶部。这就是为什么你只看到最后一个,因为它一直在所有其他的上面。我刚才意识到了这一点,并实际更改了ID!让我更新我的代码,这样你就可以see@PedroOliveira完成。我这样做了,但仍然只得到数组中的最后一个文本视图,在更改ID后,setText不起作用/您不应该在这样的视图上设置id。Id必须是唯一的数字。参考View.generateviewdhello你在吗@卡拉斯塔比尔酒店