如何在活动结合得到抽屉头布局视图? [英] How to get view from drawer header layout with binding in activity?

查看:210
本文介绍了如何在活动结合得到抽屉头布局视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我activity_main.xml中:

 <?XML版本=1.0编码=UTF-8&GT?;
<布局的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        工具:上下文=com.njiuko.balrog.balrog.com.njiuko.balrog.balrog.activities.MainActivity
    >
    &下; - !一个DrawerLayout旨在被用作使用两个宽度和高度match_parent消耗可使用的全部空间的顶层内容图。 - >
    < android.support.v4.widget.DrawerLayout
        机器人:ID =@ + ID / drawer_layout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        >        <! - 为主要内容的视图,该视图下方消耗约占整个
             使用match_parent在两个维度可用空间。 - >        < RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent>            <的LinearLayout
                机器人:ID =@ + ID / ll_container
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent
                机器人:方向=垂直>                < android.support.v7.widget.Toolbar
                    机器人:ID =@ + ID / my_awesome_toolbar
                    机器人:layout_width =match_parent
                    机器人:layout_height =WRAP_CONTENT
                    机器人:背景=@机器人:彩色/黑白
                    机器人:fitsSystemWindows =真
                    >                    <的TextView
                        机器人:ID =@ + ID / toolbar_title
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:layout_marginLeft =10dp
                        机器人:layout_marginStart =10dp
                        机器人:文字颜色=@机器人:彩色/白
                        机器人:TEXTSIZE =@扪/ abc_text_size_title_material_toolbar
                        工具:文本=@字符串/ default_toolbar_title/>                < /android.support.v7.widget.Toolbar>
                <的FrameLayout
                    机器人:ID =@ + ID /容器
                    机器人:layout_width =match_parent
                    机器人:layout_height =match_parent>                < /&的FrameLayout GT;
            < / LinearLayout中>            < android.support.design.widget.FloatingActionButton
                机器人:ID =@ + ID / fab_fuf
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignParentBottom =真
                机器人:layout_alignParentEnd =真
                机器人:layout_alignParentRight =真
                机器人:layout_marginBottom =20dp
                机器人:layout_marginEnd =20dp
                机器人:layout_marginRight =20dp
                机器人:SRC =@绘制/ Flamme的
                应用:fabSize =正常
                />
        < / RelativeLayout的>        < android.support.design.widget.NavigationView
            机器人:ID =@ + ID / navigation_view
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =match_parent
            机器人:layout_gravity =开始
            机器人:背景=@机器人:彩色/黑白
            **应用:headerLayout =@布局/ drawer_header**
            应用:itemTextColor =@彩色/ drawer_item_color_selector
            应用:菜单=@菜单/ menu_drawer/>    < /android.support.v4.widget.DrawerLayout>
< /布局>

和我使用的活动绑定,所以我没有使用findViewById其丢等这样的:

  ActivityMainBinding结合= DataBindingUtil.setContentView(这一点,R.layout.activity_main);        工具条工具栏= binding.myAwesomeToolbar;
        toolbarTitle = binding.toolbarTitle;
        BalrogFontsHelper.SetKhandBoldToView(toolbarTitle);
        setSupportActionBar(工具栏);
        最后的动作条动作条= getSupportActionBar();
        如果(动作条!= NULL){
            actionBar.setHomeAsUpIndicator(R.drawable.ic_dehaze_white_24);
            actionBar.setDisplayHomeAsUpEnabled(真);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            actionBar.setDisplayShowTitleEnabled(假);
        }
        drawerLayout = binding.drawerLayout;
        ** tvLoggedUserEmail =(的TextView)findViewById(R.id.tv_logged_user_email)**
        BalrogFontsHelper.SetKhandBoldToView(tvLoggedUserEmail);

正如你所看到的,我可以通过绑定,但是当我试图获取视图是不存在的,我不能看到绑定对象变量是直接在activity_main.xml中布局的意见。

drawer_header.xml:

 <?XML版本=1.0编码=UTF-8&GT?;<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                机器人:layout_width =match_parent
                机器人:layout_height =96dp
                的xmlns:工具=htt​​p://schemas.android.com/tool​​s
                机器人:背景=@机器人:彩色/黑白
                机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark>
    <的TextView
        机器人:ID =@ + ID / tv_logged_user_email
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerVertical =真
        机器人:layout_marginLeft =16DP
        工具:文本=@字符串/ login_placeholder_email
        机器人:textAllCaps =真
        机器人:textAppearance =@风格/ TextAppearance.AppCompat.Body2
        机器人:TEXTSIZE =20SP/>
< / RelativeLayout的>

我怎么能在有约束力的方式得到这个tv_logged_user_email TextView的,所以我有:

  ** tvLoggedUserEmail = binding.tvLoggedUserEmail **


解决方案

13/11/2015

<一个href=\"http://developer.android.com/tools/support-library/index.html\">http://developer.android.com/tools/support-library/index.html

更新设计支持库23.1.0至23.1.1

的设计支持库的变化:
添加了getHeaderView方法将NavigationView类。
修正了运行Android 4.0(API级别15)和较低的设备上FloatingActionButton对象的透明背景的问题。 (第183315)

===================

我不知道为什么没有提供其观点标题的方法
通过编程方式连接

1.solution

  NavigationView navigationView =(NavigationView)findViewById(R.id.navigation);
查看headerView = navigationView.inflateHeaderView(R.layout.header_layout)
ImageView的IV =(ImageView的)headerview.findViewById(...)

2.解决方案

  NavigationView navigationView =(NavigationView)findViewById(R.id.navigation);
查看headerView = LayoutInflater.from(本).inflate(R.layout.header_layout,navigationView,FALSE);
navigationView.addHeaderView(视图);ImageView的IV =(ImageView的)headerView.findViewById(...)

So this is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
        tools:context="com.njiuko.balrog.balrog.com.njiuko.balrog.balrog.activities.MainActivity"
    >
    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/ll_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/my_awesome_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/black"
                    android:fitsSystemWindows="true"
                    >

                    <TextView
                        android:id="@+id/toolbar_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginStart="10dp"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/abc_text_size_title_material_toolbar"
                        tools:text="@string/default_toolbar_title"/>

                </android.support.v7.widget.Toolbar>


                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </FrameLayout>
            </LinearLayout>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_fuf"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginRight="20dp"
                android:src="@drawable/flamme"
                app:fabSize="normal"
                />
        </RelativeLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/black"
            **app:headerLayout="@layout/drawer_header"**
            app:itemTextColor="@color/drawer_item_color_selector"
            app:menu="@menu/menu_drawer"/>

    </android.support.v4.widget.DrawerLayout>
</layout>

and I am using binding for the activity so I don't have to use the findViewById and cast it etc.. like this:

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        Toolbar toolbar = binding.myAwesomeToolbar;
        toolbarTitle = binding.toolbarTitle;
        BalrogFontsHelper.SetKhandBoldToView(toolbarTitle);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_dehaze_white_24);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            actionBar.setDisplayShowTitleEnabled(false);
        }


        drawerLayout = binding.drawerLayout;
        **tvLoggedUserEmail = (TextView) findViewById(R.id.tv_logged_user_email);**
        BalrogFontsHelper.SetKhandBoldToView(tvLoggedUserEmail);

As you can see, I can get the views that are directly in the activity_main.xml layout by binding but when the view I am trying to get is not there I can't see the variable in the binding object.

drawer_header.xml:

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="96dp"
                xmlns:tools="http://schemas.android.com/tools"
                android:background="@android:color/black"
                android:theme="@style/ThemeOverlay.AppCompat.Dark">


    <TextView
        android:id="@+id/tv_logged_user_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        tools:text="@string/login_placeholder_email"
        android:textAllCaps="true"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        android:textSize="20sp"/>


</RelativeLayout>

How could I get this tv_logged_user_email TextView in a binding way so I have:

**tvLoggedUserEmail = binding.tvLoggedUserEmail;**

解决方案

13/11/2015

http://developer.android.com/tools/support-library/index.html

update design support library 23.1.0 to 23.1.1

Changes for Design Support library: Added the getHeaderView method to the NavigationView class. Fixed a transparent background issue for a FloatingActionButton object on devices running Android 4.0 (API level 15) and lower. (Issue 183315)

===================

I don't know why there is no method which providing header view attached by programmatically

1.solution

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = navigationView.inflateHeaderView(R.layout.header_layout)
ImageView iv = (ImageView)headerview.findViewById(...)

2.solution

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, navigationView, false);
navigationView.addHeaderView(view);

ImageView iv = (ImageView) headerView.findViewById(...)

这篇关于如何在活动结合得到抽屉头布局视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆