android – 更新标题文本后,NavigationView标题中的重复值

在NavigationView标题中更新我的标题文本和图像后,我在NavigationView标题中获得重复值.

这是我的代码部分

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

boolean doubleBackToExitpressedOnce = false;
sqliteHelper dbHelper;
String setName,setMail;
AlertDialog.Builder builder,builder_verify;
public static int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    dbHelper = new sqliteHelper(getApplicationContext());
    builder_verify = new AlertDialog.Builder(this);        

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();     

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    View hView =  navigationView.inflateHeaderView(R.layout.nav_header_main);
    ImageView iv = (ImageView) hView.findViewById(R.id.imageView);
    TextView headerName = (TextView) hView.findViewById(R.id.txtName);
    TextView headerMail = (TextView) hView.findViewById(R.id.textEmail);

    iv.setimageResource(R.drawable.logo);
    headerName.setText("TEST");
    headerMail.setText("[email protected]");        
}    
}

在上面的代码中,我添加了视图来更新我的标题文本.我还在下面添加了主要的XML代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

<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:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

添加了导航标题布局

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
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">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:src="@drawable/logo"/>

<TextView
    android:id="@+id/txtName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="Sample"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sample Inc." /></LinearLayout>

我也添加了截图供参考

现在,我没有看到更改标题视图的任何可能性.有没有避免重复标题文字图片的建议?

非常感谢!

解决方法

好问题.

修复是通过navigationView.inflateHeaderView(R.layout.nav_header_main)而不是膨胀标题(它已经膨胀了!);通过getHeaderView(int index)方法从NavigationView获取它,然后填充它.

这是运行的代码

navigationView.setNavigationItemSelectedListener(this);
    View hView =  navigationView.getHeaderView(0);
    ImageView iv = (ImageView) hView.findViewById(R.id.imageView);
    TextView headerName = (TextView) hView.findViewById(R.id.txtName);
    TextView headerMail = (TextView) hView.findViewById(R.id.txtEmail);
    iv.setimageResource(R.drawable.logo);

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...