Android设备Toolbar显示异常求助:AVD与布局编辑器效果不符
Hey there, let's figure out why your Toolbar works perfectly in the layout editor but doesn't match on the AVD. Looking through your code, I've spotted a couple of critical issues causing this mismatch:
1. Your AndroidManifest.xml has the wrong content
You accidentally pasted the activity_main.xml layout code into your AndroidManifest.xml — that's a big problem! The manifest file defines your app's core structure and components, not UI layouts.
Here's what a correct basic AndroidManifest.xml should look like (update the package name and theme to match your project):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" <!-- Critical: Use a theme without a default ActionBar --> android:theme="@style/Theme.MyApplication"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
2. Your app theme must disable the default ActionBar
If your app's theme includes a built-in ActionBar, it will conflict with your custom Toolbar — this is the most common reason for the layout editor vs AVD discrepancy.
Open res/values/styles.xml and ensure your theme inherits from a NoActionBar variant:
<resources> <!-- Base application theme --> <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- Primary brand color --> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> <!-- Optional: Match status bar color to your Toolbar --> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> </style> </resources>
3. Small cleanup for clarity (optional but recommended)
Your menu file is named style.xml — this is confusing because it clashes with the app's styles file. Rename it to something like menu_main.xml and update the reference in activity_main.xml:
app:menu="@menu/menu_main"
Quick fix checklist:
- Replace your broken
AndroidManifest.xmlwith the correct structure above - Switch to a
NoActionBartheme in your styles and manifest - Rebuild your project (not just run) to apply all changes
After making these adjustments, run your app on the AVD again — your custom Toolbar should now look exactly like what you see in the layout editor!
内容的提问来源于stack exchange,提问作者AdorableParker

