【问题标题】:How to hide action bar on HTC one using Holo.NoActionBar theme如何使用 Holo.NoActionBar 主题隐藏 HTC One 上的操作栏
【发布时间】:2014-08-17 22:14:29
【问题描述】:

我的应用使用 android them Holo.NoActionBar 主题来隐藏操作栏并显示活动。

这在模拟器和我的 Nexus 4 上运行良好,设备操作栏被隐藏。

但是,当它在 HTC one 上运行时,它会忽略 android 主题设置并显示操作栏。它也可能显示在其他一些具有自定义 OEM 皮肤的三星或索尼设备上。我已确保活动 java 文件或 android 清单文件中没有对操作栏或菜单选项的引用。

所以我用谷歌搜索了如何隐藏操作栏并尝试了这个How to hide action bar before activity is created, and then show it again?

并编写代码

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.activity_my);

http://developer.android.com/guide/topics/ui/actionbar.html

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.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>

t1.java,没有 Actionbar.hide()

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;


public class t1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_t1);

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.rel1);
    rl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent newactivity= new Intent(getApplicationContext(), t2.class);
            startActivity(newactivity);
        }
    });
}


}

和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>

我如何选择主题:http://s16.postimg.org/6vuyjhwth/noactionbar.png

我没有创建任何其他 style.xml 文件,我错过了什么吗?如果我确实错过了一些东西,它在 nexus 4 上如何正常工作,为什么它在 IDE 中没有操作栏显示?

我的应用使用 SDK 版本 15 及更高版本

【问题讨论】:

  • 您可能应该首先提出一个问题,以弄清楚为什么您的主题不能正常工作(对于许多其他开发人员来说也是如此)。
  • @ianhanniballake:主题在 nexus 设备和模拟器上运行,我遇到的问题是在 HTC 上,可能是由于一些自定义皮肤。
  • 是的,这就是你所说的。你应该首先解决这个问题,而不是试图用创可贴来解决它。显然它适用于数以千计的其他开发人员,因此我们很想找出您的情况出了什么问题。
  • 在我们找出问题所在之前,您需要包含一堆您编写的实际代码 - 特别是您的 AndroidManifest.xml、您的样式以及您的 Activity 扩展的类。跨度>
  • 请在@style/AppTheme 的定义位置发布

标签: java android android-actionbar android-theme android-styles


【解决方案1】:

您的活动t1AndroidManifest.xml 中没有应用任何主题,因此它使用您的应用程序的默认值——在您的情况下,@style/AppTheme 包括一个操作栏。在 Manifest 中设置主题:

<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>

【讨论】:

  • 非常感谢您的帮助。当你在 cmets 中询问 styles.xml 时,我发现我错过了什么,我修改了我的代码,它现在可以工作了
  • 但是出于好奇,如果活动 t1 在 AndroidManifest.xml 中没有应用任何主题,因此它使用默认的 @style/AppTheme 包括一个操作栏,我选择了没有操作栏layout_t1.xml 设计选项中的主题但没有更新清单,为什么 nexus 4 和 HTC one 上的应用程序行为存在差异
【解决方案2】:

我能够解决问题(感谢 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 上运行良好,非常感谢 ianhanniballake

【讨论】:

  • 确保您使用的是Holo.Light.NoActionBar 而不是Holo.NoActionBar,否则其他格式(例如文本着色、对话框等)将与您的其他活动不同,除非您特别想要这样做跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多