是否可以将列表视图置于滚动视图下。我的页面上有很多项目无法显示在屏幕上,所以我使用了滚动视图。然后,我想要一个列表视图在所有这些项目下放置注释。
我已经做了一个黑客来测量listview的高度并将其放置在scrollview内,但是我知道这很不满意,有没有办法我可以将其放置在scrollview的下面?示例代码会很棒。我尝试将其放置在XML下,但未在屏幕上显示。谢谢!
<ScrollView
android:id="@+id/tabone2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backrepeat"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_gravity="center_vertical"
android:src="@drawable/mainimage" >
</ImageView>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909" />
<TextView
android:id="@+id/Description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal"
android:text="TITLE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" >
</TextView>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909" />
<TextView
android:id="@+id/tvTrack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:text="Trackname"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909" />
<TextView
android:id="@+id/ratetitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal"
android:text="RATE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" >
</TextView>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909" />
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:paddingBottom="6dp"
android:paddingTop="10dip"
android:stepSize="0.5" />
<Button
android:id="@+id/ratingbutton"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginBottom="10dip"
android:layout_marginTop="7dip"
android:background="@drawable/pagebutton"
android:gravity="center_horizontal|center_vertical"
android:text="Submit Rating"
android:textStyle="bold"
android:textSize="10sp" >
</Button>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909" />
<TextView
android:id="@+id/favorite"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal"
android:text="FAVORITE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" >
</TextView>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909"
android:layout_marginBottom="10dip" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip">
<Button
android:id="@+id/favoritebutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:background="@drawable/pagebutton"
android:gravity="center_horizontal|center_vertical"
android:text="Add to Favorites"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:textStyle="bold"
>
</Button>
<ProgressBar
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/progress_bar5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:layout_gravity="center_horizontal|center_vertical"
android:indeterminate="true"
android:visibility="gone" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909" />
<TextView
android:id="@+id/Description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal"
android:text="DETAILS"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" >
</TextView>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909"
android:layout_marginBottom="10dip" />
<TextView
android:id="@+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:gravity="center_horizontal"
android:text="Tracktime"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvViews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvRatings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvPlays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvDownloads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginBottom="10dip"
android:textStyle="bold" >
</TextView>
<View
android:id="@+id/viewdesc1"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909" />
<TextView
android:id="@+id/Description2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal"
android:text="DESCRIPTION"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" >
</TextView>
<View
android:id="@+id/viewdesc2"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909"
android:layout_marginBottom="10dip" />
<TextView
android:id="@+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:layout_marginBottom="10dip"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<Button
android:id="@+id/playbutton1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:layout_marginTop="7dip"
android:background="@drawable/pagebutton"
android:text="PLAY"
android:textStyle="bold" >
</Button>
<Button
android:id="@+id/downloadbutton3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:layout_marginTop="7dip"
android:background="@drawable/pagebutton"
android:text="DOWNLOAD"
android:textStyle="bold" >
</Button>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909" />
<TextView
android:id="@+id/tvComments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal"
android:text="COMMENTS"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" >
</TextView>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#b00909"
android:layout_marginBottom="10dip" />
<ListView
android:id="@+id/lvComments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:listSelector="#00000000" />
<EditText
android:id="@+id/etComments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:hint="Add comment..."
android:singleLine="false"/>
<Button
android:id="@+id/btnAddComment"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:layout_marginTop="7dip"
android:background="@drawable/pagebutton"
android:text="Post Comment"
android:textStyle="bold" >
</Button>
</LinearLayout>
</ScrollView>
我没有包括ViewFlipper部分...
最佳答案
使用这个方法
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}