[ http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/ ] [ブログ]を参考に、Google デザイン ライブラリを使用して Toolbar と TabLayout を実装しようとしていました。
出力は Lollipop デバイスでは期待どおりに機能しますが、Kitkat デバイスでは ToolBar と TabLayout が表示されません。しかし、kitkat デバイスでも期待どおりに 3 つのフラグメントをスワイプできます。Google サポート ライブラリを使用して記述された同じコードが、デバイスによって動作が異なるのはなぜですか。
[ Android 4.X デバイスでツールバーが表示されない [解決済み]の質問を参照してみましたが、問題は解決しませんでした。API 19を使用してエミュレーターでコードを実行しようとしましたが、同じ問題に直面しています。
'com.android.support:appcompat-v7:22.2.0'
プロジェクトに、'com.android.support:support-v4:22.2.0'
および'com.android.support:design:22.2.0'
依存関係を追加しました。
activity_main.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/tools">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Dark" />
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
public ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new QueuedFragment(), "Queued");
adapter.addFrag(new IntransitFragment(), "InTransit");
adapter.addFrag(new DeliveredFragment(), "Delivered");
viewPager.setAdapter(adapter);
}
}
style.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>