简体   繁体   English

自定义NavigationView-添加动态headerView,Android支持设计库

[英]Customising NavigationView - Adding dynamic headerView, Android Support Design Library

I tried the navigationView from the new android support design library. 我尝试了新的android支持设计库中的navigationView。 I want to have a dynamic headerview. 我想要一个动态的headerview。 Basically, my headerview will show something like quote of the day. 基本上,我的headerview将显示类似当天的报价。 I have like around 10 quotes and i want to randomly select a quote and display in a textview in the headerView. 我喜欢大约10个引号,我想随机选择一个引号并显示在headerView的textview中。 I also want to add onClick method for the headerView. 我还想为headerView添加onClick方法。

Right now, I don't see any possibilities of changing the headerview layout programmatically. 现在,我看不到以编程方式更改headerview布局的任何可能性。 Any suggestions to implement this? 有什么实施建议吗?

first create header XML like lay_header.xml 首先创建标头XML,例如lay_header.xml

<TextView
    android:id="@+id/tvThought"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

on your java file inflate this above header in a TextView. 在您的Java文件上,在TextView中将以上标头填充。 like 喜欢

TextView headerView = (TextView) LayoutInflater.from(this).inflate(R.layout.lay_header, null);
headerView.setText("Your_thoght");

Now add it as a HeaderView 现在将其添加为HeaderView

navView = (NavigationView) findViewById(R.id.navView);
navView.addHeaderView(headerView);

Thats it... 而已...

After the new support library update (23.1.1), 新的支持库更新(23.1.1)之后,

You could do this - 您可以这样做-

Add the headerview in the app:headerLayout="@layout/drawer_header" inside NavigationView. 将标题视图添加到NavigationView内的app:headerLayout="@layout/drawer_header"

Then, you can access it by, 然后,您可以通过

View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);

or if you have multiple headers 或者如果您有多个标题

navigationView.getHeaderCount()

Ref : https://code.google.com/p/android/issues/detail?id=190226#c31 参考: https : //code.google.com/p/android/issues/detail? id = 190226# c31

TextView txt2;
txt2 = (TextView) navigationView.inflateHeaderView(R.layout.nav_header_main).findViewById(R.id.textView2);
txt2.setText("wow! It works like a charm");

在此处输入图片说明

create header layout take text view inside, 创建标题布局以在其中显示文本视图,

<TextView
                android:id="@+id/profile_email_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/profile_image"
                android:layout_alignParentBottom="true"
                android:layout_toLeftOf="@id/expand_account_box_indicator"
                android:ellipsize="end"
                android:maxLines="1"
                android:paddingBottom="16dp"
                android:singleLine="true"
                android:clickable="true"
                android:onClick="onSelectText"
                android:text="dhaval0122@gmail.com"
                android:textColor="@color/body_text_2_inverse"
                android:textSize="@dimen/text_size_medium" />

in onCreate, 在onCreate中,

((TextView) findViewById(R.id.profile_email_text)).setText("test");

create method onSelectText in your activity 在您的活动中创建方法onSelectText

public void onSelectText(View v){
        if(v.getId() == R.id.profile_email_text){
            Snackbar
                    .make(fab, "clicked on sub title", Snackbar.LENGTH_LONG)
                            //.setAction(R.string.snackbar_action, myOnClickListener)
                    .show();
            drawer_layout.closeDrawers();
        }
    }

您可以通过在navigationView上调用addHeaderView以编程方式添加自定义标头,或使用app:headerLayout="@layout/myheader"在布局文件中对其进行定义。

You can use findViewById() to access header elements within the NavigationView. 您可以使用findViewById()访问NavigationView中的标题元素。 This works even if you've initialised the header with the headerLayout property eg app:headerLayout="@layout/drawer_header" . 即使您已使用headerLayout属性初始化标题(例如app:headerLayout="@layout/drawer_header" ,该方法仍然app:headerLayout="@layout/drawer_header" You can then dynamically modify the header without any need to inflate or add a new header. 然后,您可以动态修改标题,而无需添加或添加新标题。

@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {

...

if(mNavItemId == R.id.drawer_item_1)
{
  View headerView = mNavigationView.findViewById(R.id.drawer_header_root);
  // Test modifying the size of the header root element (FrameLayout)
  // when the first menu item is clicked.
  LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) headerView.getLayoutParams();
  p.height = p.height == 700 ? 400 : 700;
  headerView.setLayoutParams(p);
  return true;
}
...

drawer_header.xml cabinet_header.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="196dp"
android:background="@color/drawer_header_bg"
android:orientation="vertical"
android:id="@+id/drawer_header_root">
...

I think Dhawal is saying the same thing, but it wasn't super clear. 我认为Dhawal在说同样的话,但这还不是很清楚。

My be this link will help you 我是这个链接将为您提供帮助

slimier question 更细的问题

final NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);

View headView = navigationView.getHeaderView(0);

((TextView) headView.findViewById(R.id.nav_title)).setText("New title");

First you have to get navigationView. 首先,您必须获得navigationView。 NavigationView navigationView =(NavigationView)findViewById(R.id.nav_view);

Then header. 然后是标题。 View header = navigationView.getHeaderView(0) Then textView. View header = navigationView.getHeaderView(0)然后是textView。 TextView text = (TextView) header.findViewById(R.id.textView); And finally you can set the text you want to display. 最后,您可以设置要显示的文本。 text.setText("Hello there");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM