我正在使用v7 Toolbar
为ActionBar
。我在工具栏中使用了带有图像的ToggleButton
。我无法捕获Toolbar
上的任何点击事件,也没有ToggleButton
和导航抽屉按钮。自定义ImageView按钮不工作在工具栏
这里是我的代码(onCreate方法):
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
mToolbar = (Toolbar)findViewById(R.id.toolbar);
tv_on= (TextView) findViewById(R.id.tv_on);
toggleButton= (ImageView)findViewById(R.id.toggle);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// displayView(0);
tv_on.setText("OFF");
checked=false;
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOff off = new FragmentOff();
fragmentTransaction.add(R.id.fragment_container, off, "HELLO");
fragmentTransaction.commit();
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checked == false) {
tv_on.setText("ON");
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOff off = new FragmentOff();
fragmentTransaction.add(R.id.fragment_container, off, "HELLO");
fragmentTransaction.commit();
checked = true;
} else {
tv_on.setText("ON");
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOn on = new FragmentOn();
fragmentTransaction.add(R.id.fragment_container, on, "HELLO");
fragmentTransaction.commit();
}
}
});
Toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#D92027"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/work_status"
android:textColor="#ffffff"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/toggle"
android:layout_toStartOf="@+id/toggle"
android:layout_marginRight="16dp"
android:id="@+id/tv_work" />
<ImageView
android:id="@+id/toggle"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/off"
android:layout_marginRight="35dp"
android:layout_marginEnd="35dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_on"
android:text="@string/off"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="5dp"
android:textColor="#ffffff"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
home_screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
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">
<include
layout="@layout/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.webninjaz.fragment.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
任何人都可以请指出我是什么做错了。即使我无法调试onClickListener。
链接告诉如何使用菜单项但我的菜单项不能用作 –
您需要将图像放入CollapsingToo工具栏中的lbarLayout bot。 –