我在开发NavigationDrawer
+ Toolbar
菜单时遇到问题。 (所有在Lollipop上都有appcompat v21)
问题在于,当我在main_activity中使用NavigationDrawer
和Toolbar
时,不响应侦听器。
这是sintax或其他任何问题吗?
这是我的项目代码:
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private ActionBarDrawerToggle toogle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.tlb_toolbar);
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
toolbar.setOnMenuItemClickListener(
new Toolbar.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item){
if (item.getItemId()==R.id.info)
{
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new fragment_2())
.addToBackStack("1")
.commit();
toolbar.getMenu().clear();
toolbar.setLogo(null);
toolbar.setTitle("F.A.Q");
toolbar.inflateMenu(R.menu.menu_second);
return true;
}
if (item.getItemId()==R.id.back) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new BlankFragment())
.addToBackStack("2")
.commit();
toolbar.getMenu().clear();
toolbar.setLogo(R.drawable.ic_launcher);
toolbar.setTitle(R.string.app_name);
toolbar.inflateMenu(R.menu.menu_main);
return true;
}
if (item.getItemId()==R.id.mes)
{
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new fragment_3())
.addToBackStack("3")
.commit();
toolbar.getMenu().clear();
toolbar.setTitle("Fragments2");
toolbar.inflateMenu(R.menu.menu_second);
}
return false;
}
});
toolbar.setLogo(R.drawable.ic_launcher);
toolbar.setTitle(R.string.app_name);
toolbar.inflateMenu(R.menu.menu_main);
toogle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open,R.string.close);
toogle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(toogle);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new BlankFragment())
.commit();
}
}
}
我在布局中使用Fragment
s。我已经阅读了Fragment
进入Toolbar
+ NavigationDrawer
布局时出现毛刺问题的证明,我有什么遗漏的吗?我该怎么办?
主要布局
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar_tb" />
<FrameLayout
android:layout_below="@+id/tlb_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
/>
<!-- MAIN CONTENT -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame5"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>
<ListView
android:id="@+id/lv_menuoverflow"
android:layout_width="260dp"
android:layout_gravity="start"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#2E2E2E"
android:dividerHeight="2dp"
android:background="#ffffff"
android:textColor="#424242"
android:entries="@array/planetes"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
提前告诉你。
P:对不起我的英文。答案 0 :(得分:1)
您的布局似乎使用了一堆不必要的容器。他们可能会阻止视图,最终消耗该区域的点击次数。
试试这个布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<LinearLayout
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar_tb"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<ListView
android:id="@+id/lv_menuoverflow"
android:layout_width="260dp"
android:layout_gravity="start"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#2E2E2E"
android:dividerHeight="2dp"
android:background="#ffffff"
android:textColor="#424242"/>
</android.support.v4.widget.DrawerLayout>