Android TitleBar仍然显示在设计视图中,尽管我在styles.xml中删除了它



我正在使用activity,所以我必须创建自己的工具栏并添加它。唯一的问题是,即使我相应地修改了styles.xml,当前默认的标题栏也没有被删除。

这是我的文件:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

我已经明确表示"Theme.AppCompat.Light.NoActionBar"

我甚至在我的一个活动课上尝试过这个:

public class ViewCars extends AppCompatActivity {
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.carspage);
        }
}

然后它崩溃了!

这是来自Android Studio的实际错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{benj.samplesapp/benj.samplesapp.ViewCars}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
                                                                        at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                        at android.os.Looper.loop(Looper.java:137)

我的android清单:

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

如何删除旧的?

我认为问题在于,您定义了一个没有操作栏的样式,但从未将其应用于您的活动——实际上,在您的清单文件中,您正在将@style/Theme.AppCompat.Light应用于整个应用程序,这就是您的活动获得操作栏的原因。因此,在您的清单文件中,将您的活动声明更改为

    <activity
         android:name=".MainActivity"
         android:label="@string/app_name" 
         android:theme:"@style/AppTheme" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

并从ViewCars类中删除this.requestWindowFeature(Window.FEATURE_NO_TITLE);

相关内容

最新更新