Android API Level 19中Toolbar不显示问题求助
我之前在Android 19(KitKat)上也踩过Toolbar死活不显示的坑,结合你描述的情况和给出的代码片段,给你几个靠谱的排查方向和解决办法:
1. 先确认主题配置是否正确
Android 19本身不支持原生Material Design控件,所以必须确保你的App主题是无默认ActionBar的AppCompat主题,不然系统自带的ActionBar会和你自定义的Toolbar冲突,要么覆盖要么导致Toolbar不显示。
在res/values/styles.xml里设置主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 可根据需求添加颜色、字体等属性 --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> </style>
同时要在AndroidManifest.xml里给对应的Activity指定这个主题:
<activity android:name=".YourActivity" android:theme="@style/AppTheme"/>
2. 检查Toolbar的布局属性与初始化代码
从你给出的代码片段看,Toolbar放在AppBarLayout里,这没问题,但要注意几个关键属性:
- Toolbar的高度建议用系统自带的
?attr/actionBarSize,避免因高度设置不当导致不显示; - 给Toolbar设置背景色,不然可能和背景融为一体看起来像没显示;
- 加上
app:popupTheme确保菜单弹窗样式兼容。
调整后的Toolbar布局示例:
<android.support.design.widget.AppBarLayout android:id="@+id/app_bar" 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="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </android.support.design.widget.AppBarLayout>
另外,必须在Activity的onCreate方法里把Toolbar设置为ActionBar:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); // 如果需要显示返回箭头,可加这句 // getSupportActionBar().setDisplayHomeAsUpEnabled(true); }
如果是在Fragment里操作,要先获取AppCompatActivity实例:
Toolbar toolbar = getView().findViewById(R.id.toolbar); ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
3. 排查RelativeLayout的视图层级问题
你用RelativeLayout作为根布局,要确保后续的内容布局不会覆盖AppBarLayout。给AppBarLayout设置id后,内容布局要加上android:layout_below="@id/app_bar"属性,避免内容顶在Toolbar上方把它挡住:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Toolbar --> </android.support.design.widget.AppBarLayout> <!-- 内容布局,必须设置layout_below --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/app_bar" android:orientation="vertical"> <!-- 内容控件 --> </LinearLayout> </RelativeLayout>
另外,给根布局和AppBarLayout加上android:fitsSystemWindows="true",可以避免系统状态栏挤压Toolbar,导致Toolbar被顶出屏幕外。
4. 确认依赖库版本兼容
Android 19对高版本的AppCompat和Design库支持不好,建议使用28.0.0版本的依赖(这是最后一个支持API19的稳定版本),在build.gradle(Module级别)里配置:
android { compileSdkVersion 28 defaultConfig { minSdkVersion 19 targetSdkVersion 28 } } dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:design:28.0.0' }
如果用的是AndroidX,对应依赖是:
implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'com.google.android.material:material:1.1.0'
同时记得把布局里的android.support前缀换成androidx或者com.google.android.material。
按照上面的步骤逐一排查,应该就能解决Android 19上Toolbar不显示的问题了。
内容的提问来源于stack exchange,提问作者Ricardo

