问题描述
我有一个带有NavigationView的DrawerLayout,如下所示
I have a DrawerLayout with NavigationView as shown below
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:openDrawer="start">
<include
layout="@layout/app_bar_dynamic_screen"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<include layout="@layout/navigation_drawer_content"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
在NavigationView中,标题视图会动态膨胀,如下所示.
In NavigationView, a header view is inflated dynamically as shown below.
View headerView = getCustomHeaderView(navigationView);
if (headerView != null) {
navigationView.addHeaderView(headerView);
}
NavigationView中包含另一个布局,该布局包含一个ExpandableListView.我需要将此布局放置在膨胀的headerView的正下方.我是通过动态获取headerView的高度并将其与"marginTop"属性设置为所包含的布局来实现的.但是,如果headerView没有静态高度(如果wrap_content),则失败(两个视图都重叠).如何有效地在NavigationView中的headerView下方设置包含的布局.请帮忙.
Another layout is included in the NavigationView which contains an ExpandableListView. I need to place this layout just below the inflated headerView. I achieved this by dynamically getting the height of the headerView and setting the same as "marginTop" property to the included layout. But this fails(both views overlaps) if the headerView doesnt have a static height(if wrap_content).How can I set the included layout just below the headerView in NavigationView in an effective way. Please help.
推荐答案
答案就这么简单.只需使用onWindowFocusChanged方法获取动态标头视图的高度(带有height wrap_content),并将其设置为NavigationView中包含的布局的margin_top属性.
The answer is as simple as that. Just get the height of the dynamic header view(with height wrap_content) using the method, onWindowFocusChanged and set it as the margin_top attribute to the included layout in NavigationView.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (navigationView.getHeaderView(0) != null) {
int headerHeight = navigationView.getHeaderView(0).getHeight();
RelativeLayout navigationParentView = (RelativeLayout) findViewById(R.id.navigation_parent_view);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params.setMargins(0,headerHeight,0,0);
navigationParentView.setLayoutParams(params);
}
}
这篇关于在导航视图android的标题视图下扩大自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!