
是否可以在设计支持库NavigationVIEw中使标题粘滞?
<androID.support.design.Widget.NavigationVIEw androID:ID="@+ID/nav_drawer" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_gravity="start" app:headerLayout="@layout/nav_header" app:menu="@menu/nav_drawer" />编辑:
到目前为止,我的尝试导致了这一点
覆盖NavigationVIEw小部件并添加新方法:
public class CustomNavigationVIEw extends NavigationVIEw {public CustomNavigationVIEw(Context context) { super(context);}public CustomNavigationVIEw(Context context, AttributeSet attrs) { super(context, attrs);}public CustomNavigationVIEw(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);}// Inflates header as a child of NavigationVIEw, on top of the normal menupublic voID createheader(int res) { LayoutInflater inflater = LayoutInflater.from(getContext());; VIEw vIEw = inflater.inflate(res, this, false); addVIEw(vIEw);}}
然后将其添加到活动的onCreate:
CustomNavigationVIEw navigationVIEw = (CustomNavigationVIEw) findVIEwByID(R.ID.your_navigation_vIEw);navigationVIEw.createheader(R.layout.your_header);这样可以达到预期的效果(如果有点不好意思)但是当菜单项位于标题下方时你仍然可以点击它们,有什么想法可以解决这个问题吗?
解决方法:
经过大量的实验,我得到了一些有用的东西……非常Hacky,我很乐意听到你需要改进的建议!
覆盖的NavigationVIEw
public class CustomNavigationVIEw extends NavigationVIEw {public CustomNavigationVIEw(Context context) { super(context);}public CustomNavigationVIEw(Context context, AttributeSet attrs) { super(context, attrs);}public CustomNavigationVIEw(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);}// Consumes touch in the NavigationVIEw so it doesn't propagate to vIEws belowpublic boolean ontouchEvent (MotionEvent me) { return true;}// Inflates header as a child of NavigationVIEwpublic voID createheader(int res) { LayoutInflater inflater = LayoutInflater.from(getContext()); VIEw vIEw = inflater.inflate(res, this, false); // Consumes touch in the header so it doesn't propagate to menu items below vIEw.setontouchListener(new OntouchListener() { @OverrIDe public boolean ontouch(VIEw v, MotionEvent event){ return true; } }); addVIEw(vIEw);}// positions and sizes the menu vIEwpublic voID sizeMenu(VIEw vIEw) { // Height of header int header_height = (int) getResources().getDimension(R.dimen.nav_header_height); // Gets required display metrics displayMetrics displayMetrics = getResources().getdisplayMetrics(); float screen_height = displayMetrics.heightPixels; // Height of menu int menu_height = (int) (screen_height - header_height); // Layout params for menu LayoutParams params = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); params.gravity = Gravity.BottOM; params.height = menu_height; vIEw.setLayoutParams(params);}}
这是在主要活动的onCreate
// Inflates the nav drawer CustomNavigationVIEw navigationVIEw = (CustomNavigationVIEw) findVIEwByID(R.ID.your_nav_vIEw); navigationVIEw.createheader(R.layout.your_nav_header); // sizes nav drawer menu so it appears under header VIEwGroup parent = (VIEwGroup) navigationVIEw; VIEw vIEw = parent.getChildAt(0); navigationVIEw.sizeMenu(vIEw); 总结 以上是内存溢出为你收集整理的android – 如何使NavigationView标头粘滞全部内容,希望文章能够帮你解决android – 如何使NavigationView标头粘滞所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)