Android-关于AndroidStudio的侧滑模板的控件问题
来源:互联网 发布:eve for mac 编辑:程序博客网 时间:2024/05/22 15:29
最近在做一个项目,昨天做到需要对侧滑模板中一个TextView设置文本的功能,结果不管是直接settext("123")还是settext(String变量)都不管用,偏偏settext下边一句Toast还能正确输出要显示的内容,也检查了不是控件绑定错了,因为之前有点击方法都正确,想了很久没有解决。今天在大神提点下明白了,是控件声明方式不对。
先看一下创建项目后的目录结构:
侧滑菜单样式:
其中activity_main.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"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <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页面是包括一个ImageView(蓝色图标位置)和一个TextView(123位置)的侧滑菜单的标题部分:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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" app:srcCompat="@android:drawable/sym_def_app_icon" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android.studio@android.com" /></LinearLayout>
因此,可以理解布局控件关系为:
所以说,如果代码中将TextView设置为“123”,需要用以下方法进行初始化:
View view=navigationView.getHeaderView(0); TextView textView = (TextView) view.findViewById(R.id.textView); textView.setText("123");可以理解为NavigationView控件和ListView很像,控件下标从0开始。
0 0
- Android-关于AndroidStudio的侧滑模板的控件问题
- 关于FormView在编辑模板下找不到控件的问题
- 关于AndroidStudio的下载安装问题
- 关于eclipse工程转到androidstudio 的问题
- AndroidStudio 关于Log打印问题的总结
- 关于AndroidStudio gradle的配置出错问题
- 关于androidstudio更新gradle遇到的问题
- 关于androidstudio突然没有提示的问题
- 关于android 控件的默认属性问题
- Banner的控件 Androidstudio 日积月累
- AndroidStudio里的常用控件
- AndroidStudio的Fetching问题
- AndroidStudio遇到的问题
- AndroidStudio的编码问题
- 关于AndroidStudio导入Module出现问题 DefaultGroovyMavenDeployer 的解决方法
- 关于AndroidStudio导入项目报错的问题
- androidstudio 中关于识别不了jdk的问题
- 关于AndroidStudio不能在xml中选择mipmap的问题
- Hibernate与Mybatis对比
- bzoj3039: 玉蟾宫
- WF公务车新需求开发(续)
- Oracle表赋查询权限
- 如何在CentOS 7服务器上安装NodeJS
- Android-关于AndroidStudio的侧滑模板的控件问题
- Exec Maven Plugin全面解析和使用示例
- 行程长度压缩算法(Run Length Encoding)
- Git flow调研
- MAC-npm更新版本
- Spring Redis(3)序列化
- 喷水装置(一)
- nginx+ffmpeg搭建流媒体服务器(直播流)
- 字符串匹配的BF算法