所以,我有这个结构的NavigationView:
<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:itemIconTint="@color/drawer_item"
app:itemTextColor="@color/drawer_item"
app:itemBackground="@drawable/drawer_item"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu" />
它位于main_activity.xml上,问题是当我尝试在headerLayout“@ layout / nav_header”中的布局中创建具有此结构的TextView时:
<LinearLayout
android:orientation="vertical"
android:background="@drawable/mat_bg1"
android:layout_height="@dimen/header_height"
android:clickable="true"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:background="@color/background_floating_material_light"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="@dimen/header_top_location_padding"
android:clickable="true"
android:id="@+id/nav_location"
android:paddingLeft="@dimen/header_left_padding"
android:paddingBottom="@dimen/header_left_padding"
android:paddingRight="@dimen/header_left_padding"
>
<TextView
android:id="@+id/locationSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="VALENCIA"
android:layout_gravity="center_horizontal"
android:foregroundGravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="@color/accentColor"/>
</LinearLayout>
[...]
问题是,当单击该布局中的TextView“locationSettings”时,我无法设置任何Clickable操作。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.nite.R.layout.activity_main);
setToolbar();
drawerLayout = (DrawerLayout) findViewById(com.nite.R.id.drawer_layout);
final NavigationView menu = (NavigationView) findViewById(com.nite.R.id.nav_view);
if (menu != null) {
setupDrawerContent(menu);
}
final View headerView = getLayoutInflater().inflate(R.layout.nav_header, menu, false);
final TextView tv = (TextView) headerView.findViewById(R.id.locationSettings);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//DO you work here
Toast.makeText(getApplicationContext(), "Hey There",Toast.LENGTH_LONG);
tv.setText("AAA");
}
});
Fragment fragment = new HomeF();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(com.nite.R.id.main_content, fragment)
.commit();
//drawerLayout.closeDrawers();
setTitle(HomeF.ARG_SECTION_TITLE);
}
知道为什么吗? 顺便说一下,我可以通过Toast从TextView中检索getText,所以我正确引用了TextView,但无法设置可点击的事件......
感谢。
答案 0 :(得分:1)
删除app:headerLayout="@layout/nav_header"
并以编程方式添加标题视图:
View headerView = getLayoutInflater().inflate(R.layout.nav_header_main, navigationView, false);
navigationView.addHeaderView(headerView);