我正在学习android并且正在尝试将android.support:design
工具栏添加到我的应用中,但我也想翻页,在网上对这个问题进行了几次研究之后,我写了这样的代码
1)activity_main.xml
<RelativeLayout
android:id="@+id/main_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
layout="@layout/toolbar"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
style="@style/MyCustomTabLayout"
android:background="@drawable/bottom_menu"
android:layout_alignParentBottom="true"
android:paddingTop="?attr/actionBarSize"
/>
<com.example.misha.myapplication.utils.CustomViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/tab_layout"/>
</RelativeLayout>
2)toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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/my_toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/primary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
>
</android.support.v7.widget.Toolbar>
接下来,在java代码中,我在MainActivity.java中编写了代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_home_on));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_my_favorit_off));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_such_on));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setBackgroundResource(R.drawable.button_background_off);
viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(), getBaseContext());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
4和我的menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
android:orderInCategory="200"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
<item
android:id="@+id/action_user"
android:orderInCategory="300"
android:title="User"
android:icon="@drawable/no_name_"
app:showAsAction="ifRoom"/>
</menu>
此外,在values / styles.xml
中 <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
</style>
在AndroidManifest.xml中
<application
android:allowBackup="true"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
我遇到麻烦 - 当我点击工具栏菜单时,什么都没发生......
我尝试在main_menu.xml中替换
<menu 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" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
但是这有同样的效果......请你帮帮我,我的错误在哪里?
更新 当我试图解决问题时,我注意到,如果我删除
viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(), getBaseContext());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
菜单将起作用。
我忘了说我正在使用智能手机和平板电脑使用API - 17进行测试,而另一部智能手机使用api 15.但是使用api 23在模拟器上进行测试 - 一切都很好。
答案 0 :(得分:1)
先生,老实说,没有任何事情发生,因为你没有实施我遇到麻烦 - 当我点击工具栏菜单时没有发生任何事情
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// this is where you handle menu clicks etc etc.
// so add a switch statement and handle clicks
switch (item.getItemId()) {
case R.id.id:
break;
default:
break;
}
return true;
}
还将此添加到您的清单
application
android:allowBackup="true"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >//this guy
答案 1 :(得分:1)
请在activity_main.xml中使用@ style / ThemeOverlay.AppCompat.ActionBar更改@ style / AppTheme.PopupOverlay。它对我有用。
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_scrollFlags="scroll|enterAlways">