我正在尝试创建一个app,其中父布局是一个可滚动的视图,我使用以下代码动态地向该父布局添加一个视图:
LinearLayout ll = (LinearLayout) findViewById(R.id.parent);
for(int i=0;i<5;i++){
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View format = inflater.inflate(R.layout.student_present_absent,null);
ll.addView(format);
}
以下是我要插入视图的视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/backgroundf"
android:weightSum="8"
android:id="@+id/takeattendanceinterim">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bLogoImage"
android:src="@drawable/icwhitehouse"
android:layout_gravity="right"
android:layout_margin="5dp"
android:background="@null"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:layout_gravity="center_vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_gravity="center_vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Class 5 Section A"
android:id="@+id/tClassAndSection"
android:textColor="@color/white"
android:gravity="center"
android:layout_marginTop="1dp"
android:textStyle="normal"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_gravity="center_vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="07 Feb 2016"
android:id="@+id/tDate"
android:textColor="@color/white"
android:gravity="center"
android:layout_marginTop="1dp"
android:textStyle="normal"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bLogOut"
android:src="@drawable/iclogoffsmallest"
android:gravity="center"
android:layout_margin="20dp"
android:background="@null"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2">
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:background="#ffffff" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="7">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4">
<ScrollView
android:id="@+id/StudentNames"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/parent">
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2">
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:background="#ffffff" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5">
<Button
android:id="@+id/bMarkAllPresent"
android:text="All Present"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:textSize="15dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:layout_gravity="center_vertical">
<Button
android:id="@+id/bMarkAllAbsent"
android:text="All Absent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.7">
<Button
android:id="@+id/bContinueToTakeAttendance"
android:text="Submit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
以下是我要插入的视图:student_present_absent.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical"
android:background="@drawable/backgroundf"
android:id="@+id/abcdef">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:id="@+id/student_present_absent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bLogoImage"
android:src="@drawable/icwhitehouse"
android:layout_gravity="right"
android:layout_margin="5dp"
android:background="@null"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:layout_gravity="center_vertical">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bLogOut"
android:src="@drawable/iclogoffsmallest"
android:gravity="center"
android:layout_margin="2dp"
android:background="@null"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bLogOut1"
android:src="@drawable/iclogoffsmallest"
android:gravity="center"
android:layout_margin="2dp"
android:background="@null"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="5dp"
android:id="@+id/whiteline1">
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:background="#ffffff" />
</LinearLayout>
</LinearLayout>
问题是代码插入了student_present_absent.xml的整个页面所以我得到了student_present_absent.xml的全屏5次,我只想让70DP的布局重复5次。
请帮忙。
答案 0 :(得分:0)
这听起来像是具有多种布局的Listview / Recyclerview的用例。
使用谷歌我发现以下链接...我没有测试它,但它看起来很擅长:
http://www.survivingwithandroid.com/2014/08/android-listview-with-multiple-row.html
答案 1 :(得分:-1)
您可以添加5个视图静态,如下所示。
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View format = inflater.inflate(R.layout.student_present_absent,null);
View format1 = inflater.inflate(R.layout.student_present_absent,null);
View format2 = inflater.inflate(R.layout.student_present_absent,null);
View format3 = inflater.inflate(R.layout.student_present_absent,null);
View format4 = inflater.inflate(R.layout.student_present_absent,null);
ll.addView(format);
ll.addView(format1);
ll.addView(format2);
ll.addView(format3);
ll.addView(format4);