如何通过活动中的绑定从抽屉标题布局中获取视图? [英] How to get view from drawer header layout with binding in activity?

查看:19
本文介绍了如何通过活动中的绑定从抽屉标题布局中获取视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的activity_main.xml:

<线性布局android:id="@+id/ll_container"android:layout_width="match_parent"android:layout_height="match_parent"机器人:方向=垂直"><android.support.v7.widget.Toolbarandroid:id="@+id/my_awesome_toolbar"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/black"机器人:fitsSystemWindows =真"><文本视图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"工具:文本=@string/default_toolbar_title"/></android.support.v7.widget.Toolbar><框架布局android:id="@+id/容器"android:layout_width="match_parent"android:layout_height="match_parent"></FrameLayout></LinearLayout><android.support.design.widget.FloatingActionButtonandroid: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"应用程序:fabSize =正常"/></RelativeLayout><android.support.design.widget.NavigationViewandroid:id="@+id/navigation_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="开始"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></布局>

并且我正在为活动使用绑定,因此我不必使用 findViewById 并对其进行强制转换等等.就像这样:

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);工具栏工具栏 = binding.myAwesomeToolbar;工具栏标题 = binding.toolbarTitle;BalrogFontsHelper.SetKhandBoldToView(toolbarTitle);setSupportActionBar(工具栏);最终 ActionBar actionBar = getSupportActionBar();如果(动作条!= 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);

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

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"><文本视图android:id="@+id/tv_logged_user_email"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:layout_centerVertical =真"android:layout_marginLeft="16dp"工具:文本=@string/login_placeholder_email"机器人:textAllCaps =真"android:textAppearance="@style/TextAppearance.AppCompat.Body2"android:textSize="20sp"/></RelativeLayout>

我怎样才能以绑定方式获得这个 tv_logged_user_email TextView,所以我有:

**tvLoggedUserEmail = binding.tvLoggedUserEmail;**

解决方案

更新的解决方案 (13/11/2015)

解决方案:将您的设计支持库更新到 23.1.1:

<块引用>

设计支持库23.1.1的变化:

  • NavigationView 类中添加了 getHeaderView 方法.
  • 修复了运行 Android 4.0(API 级别 15)及更低版本的设备上 FloatingActionButton 对象的透明背景问题.(问题 183315)

参见 https://developer.android.com/tools/support-library/index.html 了解更多信息


原方案

我不知道为什么没有提供标题视图的方法以编程方式附加.

相反,这里有两个解决方案:

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

或者:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);查看 headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, navigationView, false);navigationView.addHeaderView(headerView);ImageView iv = (ImageView) headerView.findViewById(R.id.yourImageView)

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="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;**

解决方案

Updated solution (13/11/2015)

Solution: Update your Design Support Library to 23.1.1:

Changes for Design Support library 23.1.1:

  • 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)

See https://developer.android.com/tools/support-library/index.html for more info


Original solution

I don't know why there is no method which provides header view attached programmatically.

Instead, here's two solutions:

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

Or:

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

ImageView iv = (ImageView) headerView.findViewById(R.id.yourImageView)

这篇关于如何通过活动中的绑定从抽屉标题布局中获取视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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