Java 如何使用Holo.NoActionBar主题在HTC one上隐藏动作栏
我的应用程序使用android theme Holo.NoActionBar主题隐藏动作栏并显示活动 这在emulator和我的Nexus4上运行良好,设备actionbar是隐藏的 但是,当它在HTC one上运行时,它会忽略android主题设置并显示操作栏。它也可能显示在其他一些三星或索尼设备上,这些设备具有定制的OEM外观。我已经确保在ActivityJava文件或android清单文件中没有对操作栏或菜单选项的引用 所以我在谷歌上搜索了如何隐藏动作栏,并尝试了这个 并编写了代码Java 如何使用Holo.NoActionBar主题在HTC one上隐藏动作栏,java,android,android-actionbar,android-theme,android-styles,Java,Android,Android Actionbar,Android Theme,Android Styles,我的应用程序使用android theme Holo.NoActionBar主题隐藏动作栏并显示活动 这在emulator和我的Nexus4上运行良好,设备actionbar是隐藏的 但是,当它在HTC one上运行时,它会忽略android主题设置并显示操作栏。它也可能显示在其他一些三星或索尼设备上,这些设备具有定制的OEM外观。我已经确保在ActivityJava文件或android清单文件中没有对操作栏或菜单选项的引用 所以我在谷歌上搜索了如何隐藏动作栏,并尝试了这个 并编写了代码 pub
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.activity_my);
及
但是,在这两种情况下,IDE都会显示一条警告,指出方法调用'actionBar.hide()'可能会产生'java.lang.NullPointerException'
“此检查分析方法控制和数据流,以报告始终为真或假的可能条件、静态证明其值为常量的表达式以及可能导致违反可空性合同的情况”
虽然我是android开发新手,但我知道不应该忽视NullPointerException
但在此活动中,隐藏操作栏很重要
如何修改此代码,使其不易出错
更新:为什么Holo.NoActionBar不能与HTC one一起使用。它仍在显示操作栏,而在nexus 4上安装相同的apk时不会显示操作栏
Android清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxxxxxx.xxxxxxxxx" >
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
.
.
lots of non-relevant activities
.
.
<activity
android:name=".Setting"
android:label="@string/title_activity_setting"
android:parentActivityName=".MyActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.xxxxxxxxx.xxxxxxxxxx.MyActivity" />
</activity>
<activity
android:name=".t1"
android:screenOrientation="portrait"
>
</activity>
<activity
android:name=".t2"
android:screenOrientation="portrait"
>
</activity>
<activity
android:name=".t3"
android:screenOrientation="portrait"
>
</activity>
<activity
android:name=".t4"
android:screenOrientation="portrait"
>
</activity>
<activity
android:name=".t5"
android:label="@string/title_activity_t5"
android:screenOrientation="portrait"
android:parentActivityName=".MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.xxxxxxxxxx.xxxxxxxxxx.MyActivity" />
</activity>
</application>
</manifest>
t2、t3、t4、t5的代码类似
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.xxxxxxxxxx.xxxxxxxx.t1"
android:background="@drawable/finalp2"
android:id="@+id/rel1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_marginTop="40dp"
android:background="@drawable/bat"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="40dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewbottom"
android:background="@drawable/bat"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="40dp"
android:layout_marginBottom="42dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@android:drawable/ic_media_pause" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView4"
android:layout_below="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_marginTop="112dp"
android:background="@drawable/ball1" />
</RelativeLayout>
我如何选择主题:
我还没有创建任何其他style.xml文件,我错过了什么吗??如果我确实错过了一些东西,它在Nexus4上是如何工作的?为什么它在IDE中没有显示动作栏
我的应用程序使用SDK版本15及更高版本我能够解决这个问题(感谢ianhanniballake提供的帮助,非常感谢) 修改的styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="newTheme" parent="android:Theme.Holo.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
和修改的android清单
<activity
android:name=".t1"
android:screenOrientation="portrait"
android:theme="newtheme"
>
</activity>
<activity
android:name=".t2"
android:screenOrientation="portrait"
android:theme="newtheme"
>
</activity>
<activity
android:name=".t3"
android:screenOrientation="portrait"
android:theme="newtheme"
>
</activity>
<activity
android:name=".t4"
android:screenOrientation="portrait"
android:theme="newtheme"
>
</activity>
代码现在在HTC one和NEXUS上运行得很好,非常感谢Hanniballake您的活动
t1
在AndroidManifest.xml
中没有应用任何主题,因此它为您的应用程序使用默认设置-在您的情况下,@style/AppTheme
,其中包括一个操作栏。在清单中设置主题:
<activity
android:name=".t1"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar"
>
</activity>
或者专门为“无操作栏”案例创建一个新主题:
<style name="AppTheme.NoActionBar" parent="android:Theme.Holo.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
并使用它:
<activity
android:name=".t1"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
>
</activity>
你可能应该首先问一个问题,弄清楚为什么你的主题没有像它应该的那样工作(对许多其他开发人员来说也是如此)。@ianhanniballake:主题在nexus设备和emulator上工作,我遇到的问题是HTC one上可能是因为一些定制的外观。是的,你是这么说的。你应该先解决这个问题,而不是试图给它带来帮助。显然,它适用于数千名其他开发人员,因此我们很想找出您的案例中的错误。在我们找出错误之前,您需要包含一堆实际编写的代码,尤其是您的AndroidManifest.xml、您的样式、,以及您的活动扩展的类别。请将@style/AppTheme
的定义发布到哪里感谢您的帮助。当你在评论中要求styles.xml时,我发现我遗漏了什么,我修改了我的代码,它现在可以工作了,但只是出于好奇,如果活动t1在AndroidManifest.xml中没有应用任何主题,因此它使用默认的@style/AppTheme,其中包括一个操作栏,我在layout_t1.xml设计选项中没有选择任何操作栏主题,但没有更新清单,为什么nexus 4和HTC ONE上的应用程序的行为有差异确保您使用的是Holo.Light.NoActionBar
而不是Holo.NoActionBar
,否则其他格式(如文本着色、对话框等)将与您的其他活动不同,除非这是您特别想要的
<style name="AppTheme.NoActionBar" parent="android:Theme.Holo.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<activity
android:name=".t1"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
>
</activity>