我的 XML 运行良好,它有一个列表视图、一组文本视图、图像视图和按钮。然后我在整个布局中添加了一个滚动视图。Listview 停止运行,然后我添加了一个自定义类以使其工作。现在一切都按预期工作,但问题是当 xml 加载时布局的顶部不可见,我必须向上滚动才能看到它的顶部。我的问题与此相同,但提供的解决方案仍然不起作用:ScrollView 不显示顶部 这是 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_default"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/navigation_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/navigation"
android:baselineAligned="false"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#057afb"
android:textSize="19sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/navigation_layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/navigation_layout"
android:baselineAligned="false"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingTop="10dp" >
<ImageView
android:id="@+id/app_icon"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="14dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/btn_install"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/app_icon"
android:layout_marginRight="36dp"
android:background="@drawable/list_apps_install_button_selector" />
<TextView
android:id="@+id/txt_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/app_icon"
android:layout_below="@+id/app_icon"
android:text="desription goes here"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/txt_desc_long"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/app_icon"
android:layout_below="@+id/app_icon"
android:text="desription goes here"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="gone"/>
<TextView
android:id="@+id/txtMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="...More"
android:textColor="#057afb" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/navigation_layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/navigation_layout1"
android:baselineAligned="false"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txt_desc"
android:layout_marginTop="15dp"
android:background="@drawable/separator_app_details" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/information"
android:layout_below="@+id/information"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:text="@string/version"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#929292" />
<TextView
android:id="@+id/txt_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_marginLeft="22dp"
android:layout_toRightOf="@+id/textView3"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:text="@string/uploader"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#929292" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/textView5"
android:text="@string/updated"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#929292" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/information"
android:layout_below="@+id/textView7"
android:text="@string/size"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#929292" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView9"
android:layout_below="@+id/textView9"
android:text="@string/compatibility"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#929292" />
<TextView
android:id="@+id/txt_uploader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView7"
android:layout_alignLeft="@+id/txt_version"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/txt_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt_uploader"
android:layout_below="@+id/txt_uploader"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/txt_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView11"
android:layout_alignLeft="@+id/txt_updated"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/txt_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView11"
android:layout_alignBottom="@+id/textView11"
android:layout_alignLeft="@+id/txt_size"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="19dp"
android:text="@string/txt_info"
android:textColor="#000000"
android:textSize="19sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/navigation_layout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/navigation_layout2"
android:layout_marginTop="15dp"
android:baselineAligned="false"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/older_versions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="10dp"
android:text="@string/old"
android:textColor="#000000"
android:textSize="19sp" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/older_versions"
android:divider="@null" >
</ListView>
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/navigation_layout3"
android:background="@drawable/separator_app_details" />
</RelativeLayout>
</ScrollView>
这是为同时创建滚动视图和列表视图功能而编写的类:
public class Utility {
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);
}
}