我試圖通過參考[http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/] [blog]]使用谷歌設計庫實現工具欄和TabLayout。工具欄和TabLayout在Android 4.4設備上不可見
輸出按預期在棒棒糖設備上工作,但它不會在Kitkat設備上顯示ToolBar和TabLayout。但是我仍然可以在kitkat設備上通過3段片段掃描。如何使用谷歌支持庫編寫的相同代碼在不同設備上的工作方式不同!
我試過參考[Toolbar is not visible on Android 4.X devices [solved]問題,但它沒有解決問題。我試着用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>
你可以發佈你的樣式文件嗎? –
我已添加樣式文件。如果您需要更多信息,請告訴我。 –
爲什麼'TabLayout'高度'match_parent'? –