定制导航视图-添加动态headerView、Android支持设计库
我尝试了新的android支持设计库中的navigationView。我想要一个动态的headerview。基本上,我的headerview会显示一些类似于当日报价的内容。我有大约10个引号,我想随机选择一个引号并显示在headerView的文本视图中。我还想为headerView添加onClick方法定制导航视图-添加动态headerView、Android支持设计库,android,navigation-drawer,android-design-library,androiddesignsupport,navigationview,Android,Navigation Drawer,Android Design Library,Androiddesignsupport,Navigationview,我尝试了新的android支持设计库中的navigationView。我想要一个动态的headerview。基本上,我的headerview会显示一些类似于当日报价的内容。我有大约10个引号,我想随机选择一个引号并显示在headerView的文本视图中。我还想为headerView添加onClick方法 现在,我看不到任何以编程方式更改headerview布局的可能性。是否有实现此功能的建议?您可以通过在navigationView上调用addHeaderView以编程方式添加自定义标题,或者使
现在,我看不到任何以编程方式更改headerview布局的可能性。是否有实现此功能的建议?您可以通过在navigationView上调用
addHeaderView
以编程方式添加自定义标题,或者使用app:headerLayout=“@layout/myheader”
在布局文件中定义它
创建标题布局,在内部查看文本
<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" />
在活动中创建方法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();
}
}
首先创建标题XML,如lay_header.XML
<TextView
android:id="@+id/tvThought"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<?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">
...
现在将其添加为HeaderView
navView = (NavigationView) findViewById(R.id.navView);
navView.addHeaderView(headerView);
就这样…您可以使用
findViewById()
访问NavigationView中的标题元素。即使您已使用headerLayout属性(例如,app:headerLayout=“@layout/drawer\u 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\u header.xml
<TextView
android:id="@+id/tvThought"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<?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">
...
...
我认为Dhawal也在说同样的话,但不是非常清楚。在新的支持库更新(23.1.1)之后
TextView txt2;
txt2 = (TextView) navigationView.inflateHeaderView(R.layout.nav_header_main).findViewById(R.id.textView2);
txt2.setText("wow! It works like a charm");
你可以这样做-
在应用程序:headerLayout=“@layout/drawer\u header”
内部导航视图中添加headerview
然后,你可以通过
View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);
或者如果您有多个标题
navigationView.getHeaderCount()
参考:我的网站。此链接将帮助您
首先,您必须获取navigationView。
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图)代码>
然后是头球。
View header=navigationView.getHeaderView(0)
然后是文本视图。
TextView text=(TextView)header.findViewById(R.id.TextView)代码>
最后,您可以设置要显示的文本。
text.setText(“你好”)代码>
final NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View headView = navigationView.getHeaderView(0);
((TextView) headView.findViewById(R.id.nav_title)).setText("New title");