导航抽屉 header 的 OnClick 不工作
OnClick for navigation drawer header not working
我的应用程序中有一个导航抽屉,其中包含一个 header 和一些列表项。 header 有一个文本视图,我想将其设为可点击,但我做不到。
为了获取此文本视图的 ID,我使用了以下代码,因为与 onCreate 中的 setContentView 相比,它位于不同的布局文件中。
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.header, null);
TextView home = (TextView) textEntryView.findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(curr_context, "SHOW", Toast.LENGTH_LONG).show();
}
});
header.xml 包含导航抽屉的 header。它有一个名为 home 的项目。我需要让它可以点击。以上代码在onCreate方法中
不要忘记在您的 TextView xml 中定义 android:clickable="true"
。
我知道这是为那些面临同样问题的人准备的。
将您的 header 布局像这样放在导航视图中
这是在activity_main.xml
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="-24dp"
app:itemTextColor="@color/black"
app:headerLayout="@layout/layout_header_profile"
app:menu="@menu/nav_menu"/>
创建一个布局,将其命名为 layout_header_profile.xml 并将其填充到您想要的视图中。
layout_header_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="178dp"
android:orientation="vertical"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<TextView
android:id="@+id/id_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Irfan"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/id_user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:text="Irfan55121@gmail.com"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<ImageView
android:id="@+id/id_profile_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:src="@mipmap/ic_profile_pic"
/>
</RelativeLayout>
那么这个 header 布局文件将只在您的 activity_main.xml
中
所以在您的 MainActivity.java 中,您可以像从 activity_main.xml 中查看视图一样声明它并对其执行操作,不需要特殊代码。
在你的 onCreate() 中这样做
TextView tvUserName = (TextView) findViewById(R.id.id_user_name);
tvUserName.setText("My Name");
tvUserName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"clicking textview",Toast.LENGTH_LONG).show();
}
});
希望它有效编码愉快。
只需将其添加到您的页眉布局 Xml 文件
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
对我来说,其他答案无效。
我试过下面的代码。
我知道为时已晚。希望这会有所帮助。
我为访问 header 所做的操作。
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerview = navigationView.getHeaderView(0);
TextView profilename = (TextView) headerview.findViewById(R.id.prof_username);
profilename.setText("your name")
为了点击 header 的视图,这里我使用了 headerview
的线性布局
LinearLayout header = (LinearLayout) headerview.findViewById(R.id.header);
header.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HomeActivity.this, "clicked", Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.START);
}
});
或者
headerview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Your code here
}
});
首先获取 header 查看
View headerView = navigationView.getHeaderView(0);
然后使用onClick监听器
headerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// code
}
});
这样试试
navigationView.getHeaderView(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// your code here.....
}
});
我的应用程序中有一个导航抽屉,其中包含一个 header 和一些列表项。 header 有一个文本视图,我想将其设为可点击,但我做不到。
为了获取此文本视图的 ID,我使用了以下代码,因为与 onCreate 中的 setContentView 相比,它位于不同的布局文件中。
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.header, null);
TextView home = (TextView) textEntryView.findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(curr_context, "SHOW", Toast.LENGTH_LONG).show();
}
});
header.xml 包含导航抽屉的 header。它有一个名为 home 的项目。我需要让它可以点击。以上代码在onCreate方法中
不要忘记在您的 TextView xml 中定义 android:clickable="true"
。
我知道这是为那些面临同样问题的人准备的。
将您的 header 布局像这样放在导航视图中
这是在activity_main.xml
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="-24dp"
app:itemTextColor="@color/black"
app:headerLayout="@layout/layout_header_profile"
app:menu="@menu/nav_menu"/>
创建一个布局,将其命名为 layout_header_profile.xml 并将其填充到您想要的视图中。
layout_header_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="178dp"
android:orientation="vertical"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<TextView
android:id="@+id/id_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Irfan"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/id_user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:text="Irfan55121@gmail.com"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<ImageView
android:id="@+id/id_profile_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:src="@mipmap/ic_profile_pic"
/>
</RelativeLayout>
那么这个 header 布局文件将只在您的 activity_main.xml
中所以在您的 MainActivity.java 中,您可以像从 activity_main.xml 中查看视图一样声明它并对其执行操作,不需要特殊代码。
在你的 onCreate() 中这样做
TextView tvUserName = (TextView) findViewById(R.id.id_user_name);
tvUserName.setText("My Name");
tvUserName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"clicking textview",Toast.LENGTH_LONG).show();
}
});
希望它有效编码愉快。
只需将其添加到您的页眉布局 Xml 文件
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
对我来说,其他答案无效。 我试过下面的代码。 我知道为时已晚。希望这会有所帮助。
我为访问 header 所做的操作。
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerview = navigationView.getHeaderView(0);
TextView profilename = (TextView) headerview.findViewById(R.id.prof_username);
profilename.setText("your name")
为了点击 header 的视图,这里我使用了 headerview
的线性布局LinearLayout header = (LinearLayout) headerview.findViewById(R.id.header);
header.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HomeActivity.this, "clicked", Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.START);
}
});
或者
headerview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Your code here
}
});
首先获取 header 查看
View headerView = navigationView.getHeaderView(0);
然后使用onClick监听器
headerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// code
}
});
这样试试
navigationView.getHeaderView(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// your code here.....
}
});