工具栏和TabLayout在Android 4.4设备上不可见
我试图通过参考[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>
<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/ThemeOverlay.AppCompat.Light"//here
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<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>
和:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowActionBarOverlay">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
或:
<style name="ParallaxTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
谢谢SilentKnight在style.xml文件中添加windowActionBarOverlay属性后,它现在工作正常。 –
如果顶层布局父项activity_main.xml不是CoordinatorLayout(是否存在FrameLayout或RelativeLayout?),则根据API版本可能会有一些错误的不同重叠。 将顶层布局父级更改为CoordinatorLayout,它将在任何地方都可以使用。
顶部布局仅为CoordinatoLayout,但由于某些编辑问题,它没有提前显示。请你现在检查一下,如果有什么不对,请告诉我 –
更改'xmlns:app =“http://schemas.android.com/tools”'到'xmlns:app =“http://schemas.android .com/apk/res-auto“' – GPack
我将它改为”xmlns:app =“http://schemas.android.com/apk/res-auto”但我仍面临同样的问题,并在添加“ item name =“windowActionBarOverlay”> false“line in style.xml文件中,正如SilentKnight所建议的,现在工作正常。 –
我的Android 4.0有问题,我解决它。怎么看。 需要将工具栏移动到更高级别。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />-->
</android.support.design.widget.CoordinatorLayout>
这发生在我身上,这是因为我使用的是CoordinatorLayout,但我不指定layout_behavior内容。所以你应该在我的情况下这样做:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:ignore="MissingPrefix">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="@color/white"
app:titleEnabled="false">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/white"
android:elevation="4dp"
app:layout_collapseMode="pin">
<TextView
android:id="@+id/toolbar_title"
fontPath="fonts/Medium-Extd.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="my toolbar title"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="14sp" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!-- add the behavior here and the toolbar apppeared on 4.4 devices in my case -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >
//...... blah blah
</RelativeLayout>
你可以发布你的样式文件吗? –
我已添加样式文件。如果您需要更多信息,请告诉我。 –
为什么'TabLayout'高度'match_parent'? –