所以,我有这个结构的 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,但无法设置可点击事件...
谢谢。