为什么这不打开 webview
Why doesnt this open the webview
我在 navigation_menu_header.xml
上有一个 ImageButton
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="@drawable/signin"
android:textAlignment="center"
/>
这是我的 main_activity class 按钮代码
public void signIn() {
ImageButton img = (ImageButton) getLayoutInflater().inflate(R.layout.navigation_menu_header,null);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, WebViewActivity.class));
}
});
}
WebViewActivity 工作正常,因为我尝试使用 mainactivity.xml 上的按钮,但因为我在另一个 xml 上有 ImageButton,它不会打开 webview。
编辑已添加 mainactivity.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/navigation_menu_header">
</android.support.v4.widget.DrawerLayout>
您需要参考导航header视图来获取按钮。
假设你的 navigationView 是 navigationView
.
首先获取header布局。
View headerLayout =
navigationView.getHeaderView(0)
然后找到这样的按钮。
ImageButton myButton = (ImageButton)headerLayout.findViewById(R.id.signIn);
然后您可以将点击侦听器设置为 myButton
。
使用这个:
NavigationView navigationView=(NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
ImageButton img =(ImageButton)headerView.findViewById(R.id.signIn);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, WebViewActivity.class));
}
});
我在 navigation_menu_header.xml
上有一个 ImageButton<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="@drawable/signin"
android:textAlignment="center"
/>
这是我的 main_activity class 按钮代码
public void signIn() {
ImageButton img = (ImageButton) getLayoutInflater().inflate(R.layout.navigation_menu_header,null);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, WebViewActivity.class));
}
});
}
WebViewActivity 工作正常,因为我尝试使用 mainactivity.xml 上的按钮,但因为我在另一个 xml 上有 ImageButton,它不会打开 webview。
编辑已添加 mainactivity.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/navigation_menu_header">
</android.support.v4.widget.DrawerLayout>
您需要参考导航header视图来获取按钮。
假设你的 navigationView 是 navigationView
.
首先获取header布局。
View headerLayout =
navigationView.getHeaderView(0)
然后找到这样的按钮。
ImageButton myButton = (ImageButton)headerLayout.findViewById(R.id.signIn);
然后您可以将点击侦听器设置为 myButton
。
使用这个:
NavigationView navigationView=(NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
ImageButton img =(ImageButton)headerView.findViewById(R.id.signIn);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, WebViewActivity.class));
}
});