CoordinatorLayout上的额外空白,带有ScrollView和ListView

时间:2016-08-13 21:33:12

标签: android android-studio android-scrollview android-coordinatorlayout nestedscrollview

我使用Android Studio模板创建了一个滚动活动,这创建了一个activity.xml,还有一个content.xml,它包含在activity.xml中,后者有一个NestedScrollView我已经从中添加了一个ListView提要一个适配器,起初它只显示了一个项目,所以我以编程方式将其大小设置为固定值,以便它实际显示所有项目,我能够毫无问题地执行此操作并且NestedScrollView包装ListView没有问题,但是我在NestedScrollView下面有一个空白区域,我通过向所有元素添加背景颜色来自CoordinatorLayout,CoordinatorLayout是activity.xml中的主标记,但我无法弄清楚如何删除此空白区域如果我在CoordinatorLayout上使用wrap_content,它实际上会将我的内容修剪为屏幕的一半。

其他值得一提的是,如果我水平旋转手机,这个空白空间没有显示,只是在垂直位置。另外,

有谁知道可能导致这种情况的原因?这不是什么大不了的事情,我甚至可能最终离开它,但这对我来说很烦人,我无法弄清楚发生了什么。

这是我的activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#00ff00" //Green
    tools:context=".VenueActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/frog"
                    android:scaleType="centerCrop"
                    />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_venue"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_favorite_white_24dp"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end" />

</android.support.design.widget.CoordinatorLayout>

这是我的content.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".VenueActivity"
    android:background="#ff0000" //Blue
    tools:showIn="@layout/activity_venue">


    <LinearLayout
        android:id="@+id/venueList"
        android:background="#ffff00" //Yellow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <SearchView
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:id="@+id/searchView"
            android:queryHint="Search venues">

        </SearchView>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/songList" />

    </LinearLayout>


</android.support.v4.widget.NestedScrollView>

最后我的java文件:

package com.example.android.myApplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class VenueActivity extends AppCompatActivity {

    //Set up my own database manually
    String[] db_ids = {"12345", "12345", "12345", "12345", "12345", "12345"};
    String[] db_songs = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"};
    int[] db_thumbs = {R.drawable.ic_place_white_24dp, R.drawable.ic_place_white_24dp,
            R.drawable.ic_place_white_24dp, R.drawable.ic_place_white_24dp,
            R.drawable.ic_place_white_24dp, R.drawable.ic_place_white_24dp};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_venue);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Toast.makeText(VenueActivity.this, getIntent().getStringExtra("venue") + " was choosen", Toast.LENGTH_SHORT).show();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Venue has been added to your favourites", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        /**
         *  LISTVIEW TO DISPLAY DIFFRENT VENUES
         */

        ListView lv = (ListView) findViewById(R.id.songList);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView selectedSong = (TextView) view.findViewById(R.id.songName);
                // TODO Third Fragment must be changed to the Venue Home or something different than this
                Toast.makeText(VenueActivity.this, selectedSong.getText(), Toast.LENGTH_SHORT).show();
                //     Intent intent = new Intent(view.getContext(),VenueActivity.class);
                //    intent.putExtra("venue", selectedVenue.getText());
                //     startActivity(intent);
            }
        });

        SongArrayAdapter ad = new SongArrayAdapter(db_ids, db_songs, db_thumbs, getApplicationContext());
        lv.setAdapter(ad);
        ListViewInScrollView.adapt(lv);

    }
}

Vertical View scrolled all the way to the bottom

Horizontal View scrolled all the way to the bottom

0 个答案:

没有答案