Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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
Android 当循环布局更平坦时,每个视图都粘在一起_Android_Xml_View_Layout Inflater - Fatal编程技术网

Android 当循环布局更平坦时,每个视图都粘在一起

Android 当循环布局更平坦时,每个视图都粘在一起,android,xml,view,layout-inflater,Android,Xml,View,Layout Inflater,我有一个布局问题。我的代码有一个LayoutFlater,它将以下xml设置为布局,并将循环几次以显示我的学校时间表。尽管我已经将relativelayout的marginBottom和Top设置为50dp,但为什么每个布局仍然保持一致 以下是xml: 这是打印屏幕: 以下是循环: LinearLayout ll = (LinearLayout)findViewById(R.id.main_table); ll.removeAllViews(); //set tabl

我有一个布局问题。我的代码有一个LayoutFlater,它将以下xml设置为布局,并将循环几次以显示我的学校时间表。尽管我已经将relativelayout的marginBottom和Top设置为50dp,但为什么每个布局仍然保持一致

以下是xml:


这是打印屏幕:

以下是循环:

LinearLayout ll = (LinearLayout)findViewById(R.id.main_table);
ll.removeAllViews();

        //set table layout
        for(int i=0;i<tableRow.size();i++)
        {
            LayoutInflater inflater = getLayoutInflater();
            View perClassTable = inflater.inflate(R.layout.perclass_table, null);
            TextView table_day = (TextView)perClassTable.findViewById(R.id.table_day);
            TextView table_date = (TextView)perClassTable.findViewById(R.id.table_date);
            TextView table_time = (TextView)perClassTable.findViewById(R.id.table_time);
            TextView table_location = (TextView)perClassTable.findViewById(R.id.table_location);
            TextView table_class = (TextView)perClassTable.findViewById(R.id.table_class);
            TextView table_subject = (TextView)perClassTable.findViewById(R.id.table_subject);
            TextView table_lecturer = (TextView)perClassTable.findViewById(R.id.table_lecturer);
            table_day.setText(date[i].substring(0, 3));
            table_date.setText(date[1].substring(4,13));
            table_time.setText(time[i]);
            table_location.setText(loca[i]);
            table_class.setText(clas[i]);
            table_subject.setText(subj[i]);
            table_lecturer.setText(lect[i]);

            //testing
            ll.addView(perClassTable);
        }
LinearLayout ll=(LinearLayout)findViewById(R.id.main_表);
ll.removeAllViews();
//设置表格布局

对于(int i=0;iA
RelativeLayout
自上而下计算它的子项。因此,如果使用类似于上面的
android:layout=“@+id/table\u date”
,它必须位于
table\u date
之后

<RelativeLayout
    android:id="@+id/left_part"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <TextView
        android:id="@+id/table_date"
        android:layout_width="match_parent"
        android:layout_height="14dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center"
        android:text="@string/table_date"
        android:textSize="@dimen/table_date_size" />

    <TextView
        android:id="@+id/table_day"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_above="@+id/table_date"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:gravity="center"
        android:text="@string/table_day"
        android:textSize="@dimen/table_day_size" />

</RelativeLayout>


在这里,我交换了文本视图。

看起来像一个列表,请查看。
如果您正确实现适配器,它甚至会回收视图!

for
循环的
末尾为膨胀视图设置一些
布局参数

    //...
    table_subject.setText(subj[i]);
    table_lecturer.setText(lect[i]);

    // set some proper LayoutParams for the inflated View which is going to be added
    // to the ll LinearLayout
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    // set some margins to distance the rows 
    lp.topMargin = 100; 
    lp.bottomMargin = 100; 
    // add the inflated view with to ll with the LayoutParams above.
    ll.addView(perClassTable, lp);
}
LayoutParams
是一个特殊类,设计用于
ViewGroup
子类,该子类通常包含布局属性,如宽度/高度、边距、布局定位规则等。
此外,您还可以通过删除所有包装
RelativeLayouts
来改进布局文件,并直接在父
RelativeLayout
中设置
textview
。如果行数很大,您可能还需要查看
ListView
小部件,就像galex在回答中所说的那样。

您可以展示一下如何实现边距?据我所知,你不希望这些框相互接触,你希望每个框之间的边距为50 DP?是的,我想在两个框之间设置一些空间。有什么想法吗?=用循环扫描你的代码,在循环中使用
LayoutInflater
膨胀布局文件?好的,我更新了~=)tableRow.size()实际上意味着需要显示多少个类。在
for
循环的末尾,这段代码是否会有所不同<代码>LinearLayout.LayoutParams lp=新建LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级,LinearLayout.LayoutParams.WRAP_内容);lp.topMargin=100;lp.bottomMargin=100;ll.addView(perClassTable,lp) //... table_subject.setText(subj[i]); table_lecturer.setText(lect[i]); // set some proper LayoutParams for the inflated View which is going to be added // to the ll LinearLayout LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // set some margins to distance the rows lp.topMargin = 100; lp.bottomMargin = 100; // add the inflated view with to ll with the LayoutParams above. ll.addView(perClassTable, lp); }