Android 即使我在 styles.xml 中删除了标题栏,它仍显示在设计视图中
Android TitleBar is still showing in design view even though I removed it in 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"
我什至在我的 activity 类:
之一中尝试过这个
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.access0(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>
如何删除旧的?
我认为问题在于您定义了一个没有操作栏的样式,但从未将它应用到您的 activity 实际上在您的清单文件中您正在将 @style/Theme.AppCompat.Light
应用到您的整个应用程序,那就是为什么你的 activity 有一个操作栏。所以在清单文件中将 activity 声明更改为
<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 class
中删除 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
我正在使用 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"
我什至在我的 activity 类:
之一中尝试过这个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.access0(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>
如何删除旧的?
我认为问题在于您定义了一个没有操作栏的样式,但从未将它应用到您的 activity 实际上在您的清单文件中您正在将 @style/Theme.AppCompat.Light
应用到您的整个应用程序,那就是为什么你的 activity 有一个操作栏。所以在清单文件中将 activity 声明更改为
<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 class
中删除this.requestWindowFeature(Window.FEATURE_NO_TITLE);