我想在Android Studio的NavigationDrawer模板中实现ScrollView。我需要做什么?如何使完整的导航菜单可滚动?我有很多选择,但我不能滚动查看所有这些Demo Image
我正在使用这个包括navigationView
中的布局,我可以轻松管理。
像这样
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
android:fitsSystemWindows="true">
<include
layout="@layout/nav_header_main_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>
nav_header_main导航
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_gray"
android:gravity="bottom">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rlTop"
android:layout_width="match_parent"
android:layout_height="88dp"
android:layout_alignParentTop="true"
android:background="@color/appYellow">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="24dp"
android:layout_marginEnd="30dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="30dp"
android:layout_marginStart="24dp"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxEms="14"
android:maxLines="2"
android:textColor="@color/app_gray"
android:textSize="15sp" />
<RelativeLayout
android:id="@+id/rlBack"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:clickable="true">
<ImageView
android:id="@+id/ivBack"
android:layout_width="10dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="centerCrop"
android:src="@drawable/back_icon_black" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlBottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/rlTop"
android:background="@color/app_gray">
<RelativeLayout
android:id="@+id/rlProfile"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="10dp">
<ImageView
android:id="@+id/ivProfile"
android:layout_width="19dp"
android:layout_height="19dp"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:src="@drawable/icon_nav_profile" />
<TextView
android:id="@+id/tvProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="@id/ivProfile"
android:layout_toRightOf="@id/ivProfile"
android:includeFontPadding="false"
android:paddingBottom="1dp"
android:paddingTop="1dp"
android:text="@string/profile"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textDirection="locale"
android:textSize="15sp" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
这是我的整个导航活动XML
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
android:fitsSystemWindows="true">
<include
layout="@layout/nav_header_main_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是避免ViewPager
的主要因素
app_bar_main_navigation
<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:background="@color/app_gray"
tools:context="com.barq.consumer.ui.home.MainNavigationActivity">
<FrameLayout
android:id="@+id/flMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
现在在MainNavigationActivity.Java中
public class MainNavigationActivity extends BaseActivity {
@BindView(R.id.drawer_layout)
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_navigation);
ButterKnife.bind(this);
//here is you can load your fragment without view pager
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.flMain, HomeFragment.newInstance(), HomeFragment.TAG)
.commit();
}
您可以手动处理其他点击以在框架布局和活动中加载其他片段以及抽屉打开和关闭,您还可以手动使用抽屉对象。希望你能理解它。
它是一个例子,您可以轻松避免菜单并在滚动视图中使用常规布局。以下是关于上述例子ULR的更多答案