Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
ScrollView没有';t在android中显示XML的上半部分_Android_Xml_Android Layout_Listview_Scrollview - Fatal编程技术网

ScrollView没有';t在android中显示XML的上半部分

ScrollView没有';t在android中显示XML的上半部分,android,xml,android-layout,listview,scrollview,Android,Xml,Android Layout,Listview,Scrollview,我的XML工作得很好,它有一个列表视图、一组文本视图、图像视图和按钮。然后我在整个布局中添加了一个滚动视图。Listview停止运行,然后我添加了一个自定义类以使其正常工作。现在一切都正常工作了,但问题是当xml加载时,布局的顶部不可见,我必须向上滚动才能看到它的顶部。 我的问题与此相同,但提供的解决方案仍然不起作用: 这是XML文件: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http:

我的XML工作得很好,它有一个列表视图、一组文本视图、图像视图和按钮。然后我在整个布局中添加了一个滚动视图。Listview停止运行,然后我添加了一个自定义类以使其正常工作。现在一切都正常工作了,但问题是当xml加载时,布局的顶部不可见,我必须向上滚动才能看到它的顶部。 我的问题与此相同,但提供的解决方案仍然不起作用: 这是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);
    }

}
公共类实用程序{
公共静态无效setListViewHeightBasedOnChildren(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null){
//前提条件
返回;
}
int totalHeight=0;
对于(int i=0;i

将其放入
OnCreate()
方法:

final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview)); scrollview.post(new Runnable() {
  @Override public void run() {
    scrollview.fullScroll(ScrollView.FOCUS_UP);
  }
});

上面的代码为您提供了一种强制将
ScrollView
滚动到上方位置的方法。这种行为甚至可能是由某些布局中的
权重
属性引起的其中的一部分比应用上述解决方案需要更多的时间,并且不能保证每次都能知道哪些部分会干扰您的初始滚动位置。

当您打开
滚动视图时,哪一部分确切地第一次可见(粘贴一些屏幕截图,仅供参考)?谢谢,请看编辑后的问题,左边的图片是错误的,右边的图片应该是正确的方式!将其放入
OnCreate
方法:
final ScrollView ScrollView=((ScrollView)findViewById(R.id.ScrollView));scrollview.post(新Runnable(){@Override public void run(){scrollview.fullScroll(scrollview.FOCUS_UP);}})嘿,这成功了!!!!,请发帖作为回答,我会接受的,万分感谢!!看了我的一些代码进行比较后,您是否可以尝试在XML中添加一个包含
滚动视图的布局?我通常定义一个
LinearLayout
来保存我的
滚动视图。我认为缺少一个容器可能是造成问题的原因。你能评论一下你的解决方案吗?为什么它能解决问题?我当然可以看到你强制聚焦,但是如果用户想要的话,难道他不能向上滚动吗?是不是因为省略了
ScrollView
的声明导致了这个问题?我会在答案中粘贴一些信息,你是对的。但是你所说的“如果用户愿意,他不应该向上滚动”和“省略ScrollView的声明”是什么意思?你说的是哪种声明?在应用你给我的解决方案之前,页面加载没有上半部分,这不是通常的方式。但在您的解决方案中,视图现在运行正常,首先加载上半部分,然后用户可以逐渐向下滚动,也可以在到达末尾时出现。所讨论的布局位于
ScrollView
中,因此这不应该是问题!我认为发生的事情是,当创建了滚动视图时,它搜索一个焦点开始,而第一个
线性布局没有
android:focusableInTouchMode=“true”
它搜索下一个最合适的候选焦点。在本例中,它是最后一个
TextView
,我认为
ScrollView
只是开始滚动到底部,这可以用它来解释(我实际上没有看到它是这样开始的,但现在我认为它开始滚动到底部)。