我有最新版本的Android Studio。在我使用Blank Activity创建一个新应用程序之后,我为MainActivity(或官方教程中提到的MyActivity)提供了2个布局。似乎有一个activity_my.xml,其中包含content_my.xml。
我只想摆脱标题栏,所以我尝试了以下内容:Trying to get rid of title bar for all activities
现在activity_my.xml看起来像这样:
<?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"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/MyTheme">
<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>
<include layout="@layout/content_my" />
</android.support.design.widget.CoordinatorLayout>
v21 / styles.xml有:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primary_dark</item>
<item name="colorAccent">@color/accent_color</item>
</style>
那不起作用。
当我去设计activity_my.xml的视图时,我仍然看到标题栏。然后我删除了
部分中的XML<android.support.design.widget.AppBarLayout> ... </android.support.design.widget.AppBarLayout>
酒吧终于消失了!!
但现在如果转到content_my.xml并转到其Design视图,我会收到一个巨大的渲染错误:
Rendering Problems
The following classes could not be instantiated:
- android.support.design.widget.CoordinatorLayout (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
Exception Details
java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library. at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34) at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:178) at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:172) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
Copy stack to clipboard
The surrounding layout (@layout/activity_my) did not actually include this layout. Remove tools:showIn=... from the root tag.
那么如何修复上一个错误呢。看来我必须以某种方式保留这个android.support.design.widget.AppBarLayout XML,但这会干扰我的标题栏免费UI:(
注意:activity_my.xml仍包含content_my.xml,因为我没有从中删除以下行:include layout =“@ layout / content_my”
所以content_my.xml应该继续正确呈现。
这是我的Module:app
的build.gradleapply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.mycompany.myapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
}
答案 0 :(得分:0)
错误很明显;确保您正在扩展AppCompatActivity
。
答案 1 :(得分:0)
好的,我终于明白了。
似乎styles.xml(不是v21 / styles.xml)必须附加它:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
然后AndroidManifest.xml应该有:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/MyTheme" >
...
然后我可以安全地删除该部分:
<android.support.design.widget.AppBarLayout> ...</android.support.design.widget.AppBarLayout>
来自activity_my.xml
这允许activity_my.xml和content_my.xml在MainActivity.xml中再次呈现而没有标题栏
设备中的渲染始终没有标题栏(正如预期的那样),无论设计库错误如何,仍然如此。