在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("test@gmail.com"); } }
在上面的代码中,我添加了视图来更新我的标题文本.我还在下面添加了主要的XML代码
还添加了导航标题布局
nav_header_main.xml
我也添加了截图供参考
现在,我没有看到更改标题视图的任何可能性.有什么建议可以避免重复的标题文字和图片?
非常感谢!
好问题.
修复是通过navigationView.inflateHeaderView(R.layout.nav_header_main);
从NavigationView
via getHeaderView(int index)
方法获取它然后填充它而不是膨胀标题(它已经膨胀了!).
这是运行的代码:
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);
在xml fie中你已经设置好了
app:headerLayout="@layout/nav_header_main"
for NavigationView
和class java中你用你的视图充气
View hView = navigationView.inflateHeaderView(R.layout.nav_header_main);
所以有一个重复的值NavigationView
.