Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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
Android 使整个布局可滚动,而不仅仅是listview_Android_Listview_Scrollable - Fatal编程技术网

Android 使整个布局可滚动,而不仅仅是listview

Android 使整个布局可滚动,而不仅仅是listview,android,listview,scrollable,Android,Listview,Scrollable,我试图使整个布局可滚动,但在这种情况下,只有listview被滚动,而不是整个布局。请告诉我如何才能使整个页面滚动起来,而不仅仅是其中的listview。谢谢 我的XML文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" and

我试图使整个布局可滚动,但在这种情况下,只有listview被滚动,而不是整个布局。请告诉我如何才能使整个页面滚动起来,而不仅仅是其中的listview。谢谢

  • 我的XML文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
    <GridView
        android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="2dp"
        android:numColumns="auto_fit"
        android:paddingTop="2dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="2dp" />
    
    </LinearLayout>
    
    <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:text="More Devices"
    android:textStyle="bold" />
    
    <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="3dp"
    android:paddingLeft="3dp"
    android:paddingTop="3dp" />
    
    </LinearLayout>
    
    
    
  • My MainActivity.java

  • package project.example.com.project

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.GridView;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    
    public class MainActivity extends Activity {
    
    GridView grid;
    String[] gridtext = {"Text 1", "Text 2", "Text 3", "Text 4", "Text    5", "Text 6", "Text 7", "Text 8", "Text 9"};
    int[] gridimage = {R.drawable.img, R.drawable.img, R.drawable.img,   R.drawable.img, R.drawable.img, R.drawable.img, R.drawable.img,   R.drawable.img, R.drawable.img};
    ListView listView;
    String[] listtext = {"List 1", "List 2", "List 3", "List 4", "List   4", "List 4"};
    int[] listimage = {R.drawable.image, R.drawable.image,  R.drawable.image, R.drawable.image, R.drawable.image, R.drawable.image};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.list);
    CustomGrid adapter = new CustomGrid(MainActivity.this, gridtext,  gridimage);
    grid = (GridView) findViewById(R.id.grid);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener()    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " +   gridtext[+position], Toast.LENGTH_SHORT).show();
        }
        });
    
        List<HashMap<String, String>> list = new  ArrayList<HashMap<String, String>>();
    
        String[] from = {"txt", "img"};
        int[] to = {R.id.list_text, R.id.list_image};
        SimpleAdapter simpleAdapter = new   SimpleAdapter(getBaseContext(), list, R.layout.list_row, from, to);
        for (int i = 0; i < listtext.length; i++) {
        HashMap<String, String> hashMap = new HashMap<String, String> ();
        hashMap.put("txt", listtext[i]);
        hashMap.put("img", Integer.toString(listimage[i]));
        list.add(hashMap);
        }
    
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new  AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int   position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " +            listtext[+position], Toast.LENGTH_SHORT).show();
        }
        });
        View header = getLayoutInflater().inflate(R.layout.header,  null);
        listView.addHeaderView(header);
    
    
        }
        }
    
    导入android.app.Activity;
    导入android.os.Bundle;
    导入android.view.view;
    导入android.widget.AdapterView;
    导入android.widget.GridView;
    导入android.widget.ListView;
    导入android.widget.simpledapter;
    导入android.widget.Toast;
    导入java.util.ArrayList;
    导入java.util.HashMap;
    导入java.util.List;
    公共类MainActivity扩展了活动{
    网格视图网格;
    字符串[]gridtext={“文本1”、“文本2”、“文本3”、“文本4”、“文本5”、“文本6”、“文本7”、“文本8”、“文本9”};
    int[]gridimage={R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img,R.drawable.img};
    列表视图列表视图;
    字符串[]listtext={“列表1”、“列表2”、“列表3”、“列表4”、“列表4”、“列表4”};
    int[]listimage={R.drawable.image,R.drawable.image,R.drawable.image,R.drawable.image,R.drawable.image,R.drawable.image};
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView=(listView)findViewById(R.id.list);
    CustomGrid adapter=新的CustomGrid(MainActivity.this、gridtext、gridimage);
    grid=(GridView)findviewbyd(R.id.grid);
    设置适配器(适配器);
    grid.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
    @凌驾
    public void onItemClick(AdapterView父级、视图、,
    内部位置,长id){
    Toast.makeText(MainActivity.this),您在“+gridtext[+position],Toast.LENGTH_SHORT.show]处单击过它();
    }
    });
    列表=新的ArrayList();
    字符串[]from={“txt”,“img”};
    int[]to={R.id.list_text,R.id.list_image};
    simpledapter simpledapter=新的simpledapter(getBaseContext(),list,R.layout.list_行,from,to);
    for(int i=0;i
    在xml文件中添加一个滚动视图,其中包含列表视图和其他要滚动的元素。ScrollView只能包含一个子视图。为了使整个屏幕可滚动,您必须在ScrollView中放置一个包含所有其他视图的布局。LinearLayout、GridLayout和RelativeLayout都可以包含多个子视图(将布局和视图嵌套在单个父布局中)。只需将滚动视图设置为整个屏幕的高度和宽度,您就可以使用它。

    在这种情况下,您只需添加一个
    滚动视图,如下所示:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
    .
    .
    .
    </ScrollView>
    
    
    .
    .
    .
    
    但一定要记住删除
    xmlns:android=”http://schemas.android.com/apk/res/android"
    xmlns:tools=”http://schemas.android.com/tools“
    ,这两行来自您的线性布局

    
    
            <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"        
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"             
            tools:context=".MainActivity" >
    
            <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
    
            <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
            <GridView
                android:id="@+id/grid"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:columnWidth="100dp"
                android:gravity="center"
                android:horizontalSpacing="2dp"
                android:numColumns="auto_fit"
                android:paddingTop="2dp"
                android:stretchMode="columnWidth"
                android:verticalSpacing="2dp" />
    
            </LinearLayout>
    
            <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="More Devices"
            android:textStyle="bold" />
    
            <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="3dp"
            android:paddingLeft="3dp"
            android:paddingTop="3dp" />
    
            </LinearLayout>
              </ScrollView>
    

    您应该将布局和视图放在ScrollView中

    <LinearLayout>
    
        <ScrollView>
    
            <LinearLayout>
                      <GridView></GridView>
             </LinearLayout>
             <TextView></TextView>
             <LinearLayout>
                      <ListView></ListView>
             </LinearLayout>
    
    
        </ScrollView>
    </LinearLayout>
    
    
    

    参考资料:

    我和你的问题完全一样。我解决问题的思路是从用户Touhid指出的开始:

    将ListView放在ScrollView中从来没有灵感。但是如果你 如果希望您发布类似XML的行为,我有3个选项:

  • 移除ScrollView:移除ScrollView,您可以为ListView提供与总布局相关的特定大小(或 特定dp或布局(重量)

  • 将ListView替换为LinearLayouts:您可以通过迭代项列表来添加列表项,并将每个项视图添加到列表中 通过膨胀视图和设置相应的 数据(字符串、图像等)

  • 如果确实需要将ListView放在ScrollView中,则必须使ListView不可滚动(这实际上是相同的) 与上面的解决方案2相同,但使用ListView代码),否则 布局不会像您期望的那样工作。创建列表视图的步骤 不可滚动,您可以阅读此内容,其中 我的解决方案如下所示

  • :

    就我而言:

  • 解决方案1-删除滚动视图-不是一个选项。创建 具有特定大小的列表视图看起来很糟糕,令人困惑

  • 我想尝试用LinearLayouts替换ListView,但结果表明,搜索结果表明:这不是一个好主意,特别是如果ListView包含很多项。最好将标题添加到ListView或使用自定义的NonScrollableListView类。由于标题在我的列表中看起来也很糟糕,我决定不这样做

  • 看来最后一个选择是给我的。我确实使我的列表视图不可滚动。而且效果很好。我找到的答案是。更准确地说,Dedaniya HirenKumar的回答是:2014年7月8日10:21。 注意不要忘记答案的最后一部分。仅仅创建不可滚动的listview java类和更改XML是不够的!!!你需要定义y