![](/img/trans.png)
[英]How to set text to view from drawer header layout in navigation drawer without inflating view
[英]How to get view from drawer header layout with binding in activity?
所以這是我的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>
我正在使用綁定活動所以我不必使用findViewById並投射它等..像這樣:
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);
正如您所看到的,我可以通過綁定直接獲取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">
<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>
我怎么能以綁定方式獲取這個tv_logged_user_email TextView所以我有:
**tvLoggedUserEmail = binding.tvLoggedUserEmail;**
解決方案:將設計支持庫更新為23.1.1
:
設計支持庫
23.1.1
更改:
- 將
getHeaderView
方法添加到NavigationView
類。- 修復了運行Android 4.0(API級別15)及更低版本的設備上的
FloatingActionButton
對象的透明背景問題。 (問題183315)
有關詳細信息,請參閱https://developer.android.com/tools/support-library/index.html
我不知道為什么沒有方法提供以編程方式附加的標題視圖。
相反,這里有兩個解決方案:
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)
要么:
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)
我有一個稍微清潔的解決方案,我不需要使用負責膨脹和向NavigationView添加標題視圖的代碼來“污染”片段/活動。 我為NavigationView實現了擴展類。 我是這樣做的:
我的活動布局:
<data>
<variable
name="dashboard"
type="ramps.view.model.DashboardScreenViewModel"/>
</data>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/activity_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
bind:dashboard="@{dashboard}"
/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:background="@color/white"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:navigationItemSelectedListener="@{dashboard.onMenuItemSelected}"
app:model="@{dashboard.score}"
app:menu="@menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
正如您所看到的,NavigationView中沒有app:headerLayout,但我添加了我的自定義應用:模型,我想要傳遞給標題布局的數據。 我是如何定義此自定義參數的? 通過擴展類:
public class NavigationViewExtensions {
@BindingAdapter({"bind:model"})
public static void loadHeader(NavigationView view, ScoreViewModel model) {
ViewNavigationHeaderBinding binding = ViewNavigationHeaderBinding.inflate(LayoutInflater.from(view.getContext()));
binding.setScore(model);
binding.executePendingBindings();
view.addHeaderView(binding.getRoot());
}
}
只需將此類放在項目的任何位置即可。 我的標題的布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
>
<data>
<import type="android.view.View"/>
<variable
name="score"
type="ramps.view.model.ScoreViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/graphite"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin_large"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin_large"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<include
android:id="@+id/player_details_header_score"
layout="@layout/view_avatar_score_header"
bind:score="@{score}"/>
</LinearLayout>
如果您設置app:headerLayout="@layout/drawer_header
那么您不必再次膨脹視圖。您可以使用.bind
而不是.inflate
。
您可以獲取已經膨脹的標題視圖並將其綁定為:
View headerView = binding.navigationView.getHeaderView(0);
DrawerHeaderBinding headerBinding = DrawerHeaderBinding.bind(headerView);
我曾經有過同樣的問題。
解決方法是膨脹標題視圖並以編程方式添加它。
像這樣:
DrawerHeaderBinding drawerHeaderBinding = DrawerHeaderBinding.inflate(LayoutInflater.from(navigationView.getContext()));
navigationView.addHeaderView(drawerHeaderBinding.getRoot());
drawerHeaderBinding.tvLoggedUserEmail = "email";
drawerHeaderBinding.executePendingBindings();
所以刪除app:headerLayout並以編程方式執行。 我認為谷歌應該在這里修復核心問題,無論是在設計庫還是在數據綁定庫中。
ImageView imageView = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.imageButton);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag) {
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.drawer_view1);
flag = false;
} else {
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.drawer_view);
flag = true;
}
}
});
這里有一個簡單的解決方案。 我們假設您將NavigationView添加為
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/my_nav_drawer_header"
app:menu="@menu/menu_nav_drawer"/>
而要在訪問一個TextView my_nav_drawer_header.xml
有一個ID textView2
。 所以你只需在你的活動中使用這個代碼:
View headerContainer = navigationView.getHeaderView(0); // This returns the container layout from your navigation drawer header layout file (e.g., the parent RelativeLayout/LinearLayout in your my_nav_drawer_header.xml file)
TextView textView2 = (TextView)headerContainer.findViewById(R.id.textView2);
textView2.setText("Sorted!");
無需誇大或干擾您現有的代碼等。
嘗試使用DataBinding庫,它適用於我。
navigation_view_header.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/navigation_view_header_height"
android:paddingLeft="@dimen/navigation_view_padding"
android:paddingTop="@dimen/navigation_view_top_padding"
android:background="@color/colorPrimary">
<ImageView
android:id="@+id/avatar"
android:layout_width="@dimen/avatar_dimen"
android:layout_height="@dimen/avatar_dimen"
android:contentDescription="@null"
android:src="@drawable/default_avatar" />
<TextView
android:id="@+id/profile_email"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@+id/avatar"
android:text="email"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:gravity="center_vertical" />
</RelativeLayout>
</layout>
activity_main.xml中:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="navigationItemSelectedListener"
type="com.example.MainActivity"/>
</data>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:tag="layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:titleTextColor="@color/white"
android:background="@color/colorPrimary"
app:theme="@style/Toolbar.Theme"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="@dimen/navigation_view_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:theme="@style/Theme.AppCompat.Light"
app:menu="@menu/drawer_menu"
app:navigationItemSelectedListener="@{navigationItemSelectedListener::onNavigationItemSelected}"/>
</android.support.v4.widget.DrawerLayout>
</layout>
在你的活動中:
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,
R.layout.activity_main);
activityMainBinding.setNavigationItemSelectedListener(this);/* with this line navigation menu item selection events are handled in onNavigationItemSelected() specified in navigation_view_header.xml*/
NavigationViewHeaderBinding navigationViewHeaderBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.navigation_view_header,activityMainBinding.navigationView,false);
activityMainBinding.navigationView.addHeaderView(navigationViewHeaderBinding.getRoot());
並確保您的活動實現NavigationView.OnNavigationItemSelectedListener
編輯gradle文件以將com.android.support庫更新到版本23.1.1或更高版本。
使用navigationView.getHeaderView(i)
,其中i
是headerview
的索引。 如果您剛剛在布局上定義了此視圖,則為0
。
我已經從Android sdk管理器更新了構建工具,然后23.1.0
也適合我。
我正在使用buildToolsVersion "23.0.2"
在此之前它是23.0.1
。
並且不需要使用:
(View) navigationView.findViewById(R.id.idOfViewFromHeaderView);
在您的活動中,您可以直接使用:
(View) findViewById(R.id.idOfViewFromHeaderView);
只需輸入navigationView.getHeaderView(0)然后使用任何視圖
TextView profile,info;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
profile = navigationView.getHeaderView(0).findViewById(R.id.profile);
info = navigationView.getHeaderView(0).findViewById(R.id.info);
profile.setOnClickListener(this);
info.setOnClickListener(this);
這個對我有用。
MainActivity.java:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
NavHeaderMainBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.nav_header_main, navigationView, false);
navigationView.addHeaderView(binding.getRoot());
activity_main.xml中:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
nav_header.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="account"
type="lonja.dreamteam.su.trainingdiary.view_model.AccountViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/header_background"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageView"
android:layout_width="72dp"
android:layout_height="72dp"
android:src="@{account.sex}"
app:civ_border_color="@color/colorAccent"
app:civ_border_width="0dp" />
<TextView
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@{account.name}"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>
你可以這樣做。 首先初始化導航視圖。
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
然后初始化navigationView中的所有視圖。
CircleImageView circleView = (CircleImageView) navigationView.findViewById(R.id.circleView);
TextView name = (TextView) navigationView.findViewById(R.id.name);
TextView email = (TextView) navigationView.findViewById(R.id.email);
LinearLayout header = (LinearLayout) navigationView.findViewById(R.id.header);
然后您可以根據需要使用這些視圖。 例如:
name.setText(NAME);
email.setText(EMAIL);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.