我有一个cardview作为父布局。在那张cardview里面,我正在夸大物品的列表视图。我现在有一个固定的cardview高度,但我想根据listview中的项目动态调整高度。
这是我的父xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/shelfShareLinearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:padding="20dp">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</ScrollView>
这是我的孩子视图的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/shelfShareMasterLL"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/frontalRodShareCardView"
android:layout_width="585dp"
android:layout_height="500dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="15dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:id="@+id/shelfShareMasterRL"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/deleteSlefShareCard"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:visibility="invisible"
android:background="@drawable/round_corner"
android:drawableTop="@drawable/ic_delete_black_24dp"
android:paddingTop="10dp" />
<TextView
android:id="@+id/shelfShareHeadingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="Frontal Rod Share"
android:textSize="20sp" />
<CheckBox
android:id="@+id/noFrontalRodShareCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/shelfShareHeadingTextView"
android:visibility="invisible"
android:layout_toLeftOf="@+id/deleteSlefShareCard"
android:layout_marginRight="20dp"
android:text="No Frontal Rod Share" />
<ListView
android:id="@+id/shelfShareProductListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/shelfShareHeadingTextView">
</ListView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这就是我如何向子视图展示并向其显示列表:
ArrayList<Products> shelfShareList = new ArrayList<Products>();
final LinearLayout shelfShareLL = (LinearLayout) view.findViewById(R.id.shelfShareLinearlayout);
final View shelfShareChildView = getActivity().getLayoutInflater().inflate(R.layout.cardviewshelfshare, null);
shelfShareLL.addView(shelfShareChildView);
ListView listView = (ListView) shelfShareChildView.findViewById(R.id.shelfShareProductListView);
Cursor crs = database.rawQuery(//My query, null);
while (crs.moveToNext()){
shelfShareList.add(//data from query);
}
listView.setAdapter(new ProductsAdapter(getActivity(), shelfShareList));
crs.close();
答案 0 :(得分:0)
如果不以编程方式将子视图xml添加到父级,请将其添加到父级xml中,如下所示:
<include
layout="@layout/layout_name_of_your_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
然后在子布局中,删除根LinearLayout
并以root身份创建CardView
。然后,请务必将layout_height
中的wrap_content
更改为CardView
。
然后将子xml文件中layout_height
的{{1}}更改为ListView
。这应该可以正常工作!