无标题栏的安卓主题

15
在我的应用程序中,我想使用Theme.NoTitleBar主题,但另一方面我也不想失去Android操作系统的内部主题。我在网上搜索并找到了以下答案。我修改了我的styles.xml文件,并添加了以下代码行。
在values/styles.xml文件中。
<style name="Theme.Default" parent="@android:style/Theme"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>

在 values-v11/styles.xml 内部

<style name="Theme.Default" parent="@android:style/Theme.Holo"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.Holo.NoActionBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen"></style>

在 values-v14/styles.xml 文件中

<style name="Theme.Default" parent="@android:style/Theme.Holo.Light"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></style>

在Manifest文件的application标签中,我添加了一个属性:

android:theme="@style/Theme.NoTitle"

但是当我尝试运行代码时,我的应用程序中的图像会变得模糊。但是当我使用以下标签:

android:theme="@android:style/Theme.NoTitleBar"
或者
android:theme="@android:style/Theme.Light.NoTitleBar"
或者
android:theme="@android:style/Theme.Black.NoTitleBar"

应用程序中的图像以正确的形式出现... 但在这种情况下,我会失去新的Android操作系统上的所有主题..

请帮助我如何在不失去图像和本地主题的情况下使用NoTitleBar主题..

布局的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<include
    android:id="@+id/main_top_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/top_bar_title" />

<RelativeLayout
    android:id="@+id/container_bar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/main_top_bar"
    android:layout_marginTop="-3dp"
    android:background="@drawable/tab_nav_bar" >
</RelativeLayout>

<RelativeLayout
    android:id="@+id/container_bar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/container_bar1"
    android:background="@drawable/location_nav_bar" >

    <TableLayout
        android:id="@+id/map_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:paddingBottom="5dp"
        android:background="@drawable/map_bar_bg" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/MapPointer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:background="@drawable/map_pointer" />

            <TextView
                android:id="@+id/MapSeperator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="2dp"
                android:text="|"
                android:textColor="#979ca0"
                android:textSize="20dp" />

            <com.pnf.myevent.CustomTextView
                android:id="@+id/DisplayLocation"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal"
                android:marqueeRepeatLimit="marquee_forever"
                android:paddingLeft="5dp"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:textColor="#adabad"
                android:textSize="12dp" />

            <Button
                android:id="@+id/RefreshBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:background="@drawable/refresh_button" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/calendar_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp" >

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/MonthBtn"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/month_button" />

            <Button
                android:id="@+id/TodayBtn"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/today_button" />

            <Button
                android:id="@+id/WeekBtn"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/week_button" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/container_bar3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/container_bar2"
    android:background="@drawable/cal_nav_bar" >

    <Button
        android:id="@+id/CalPrevious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/left_arrow_button" />

    <com.pnf.myevent.CustomTextView
        android:id="@+id/CalTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:shadowColor="#ffffff"
        android:shadowDx="0"
        android:shadowDy="2"
        android:shadowRadius="1"
        android:text="Title"
        android:textColor="#666666"
        android:textSize="15dp" />

    <Button
        android:id="@+id/CalNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/right_arrow_button" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/container_bar4"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_below="@+id/container_bar3"
    android:background="#c8c9cc" >

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:horizontalSpacing="2dp"
        android:listSelector="#00000000"
        android:numColumns="7"
        android:stretchMode="columnWidth"
        android:verticalSpacing="2dp" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/footer_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/container_bar4" >

    <ListView
        android:id="@+id/CalendarList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="#00000000"
        android:cacheColorHint="#00000000"
        android:divider="#dedede"
        android:dividerHeight="1dp"
        android:drawSelectorOnTop="false" />
</RelativeLayout>

Images Getting Blurry


如果设置无标题栏,您不会失去Android主题。您在XML布局中做错了什么,请发布布局以便我们进行审核。此外,这是否发生在所有Android平台版本上?还是只发生在特定的一个版本上? - Ifrit
它在所有平台上都发生了。我已经在红色文件夹中创建了布局文件夹,如layout-ldpi、layout-mdpi、layout-hdpi和layout-xhdpi,并且还有包含不同屏幕的不同图像的drawable文件夹。截图是从Galaxy S3上拍摄的,但在模拟器上也是一样的。 - Abhishek Dhiman
你能发布其中一个布局吗?除非你有一些特殊情况,否则你不需要根据dpi使用不同的布局。 - Ifrit
@JaySoyer 我已经编辑了帖子,请看一下... - Abhishek Dhiman
好的。我的下一个问题是,您是否在使用drawable-mdpi、drawable-hdpi等相应文件夹?那个XML布局特别为哪个布局设计的呢? - Ifrit
是的,我正在使用drawable-mdpi等。此布局正在使用drawable-mdpi。 - Abhishek Dhiman
7个回答

30

如果您想保留原始的UI风格并移除标题栏而不影响它,您需要在活动中删除标题栏,而不是在清单中删除。保留清单中的原始主题样式,并在每个您想要无标题栏的活动中使用this.requestWindowFeature(Window.FEATURE_NO_TITLE);onCreate()方法之前setContentView() ,像这样:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_signup);
    ...
}

7
这个方法可行,但不是最佳方案。标题栏会在消失前闪烁几秒钟。 - Andriy Kopachevskyy
1
谢谢,它对我起作用了。点赞。 - user4050065
我按照完全相同的顺序使用了同样的代码行,但是我仍然遇到了异常:“android.util.AndroidRuntimeException:必须在添加内容之前调用requestFeature()” - 有人知道为什么吗? - GyRo

24

要隐藏操作栏,请在Values/Styles中添加以下代码

<style name="CustomActivityThemeNoActionBar" parent="@android:style/Theme.Holo.Light">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

然后在您的AndroidManifest.xml文件中,在所需的活动中添加以下代码

<activity
        android:name="com.newbelievers.android.NBMenu"
        android:label="@string/title_activity_nbmenu"
        android:theme="@style/CustomActivityThemeNoActionBar">
</activity>

4
由于"android:windowActionBar"要求API +11,而我的兼容性是+8,所以"android:windowNoTitle"就可以解决问题了。 谢谢 ;) - Edison Spencer

7
在你的清单文件中使用如下内容:-
    android:theme="@style/AppTheme" >

在 styles.xml 文件中:-
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
       <item name="android:windowActionBar">false</item>
   <item name="android:windowNoTitle">true</item>
</style>

令人惊讶的是,这个操作按您所希望的方式起作用,但在AppTheme中使用与AppBaseTheme相同的父级却不行。


1
惊喜惊喜,这个页面上的所有内容都对我没用。我怀疑我必须停止扩展ActionBarActivity,转而使用原始的Activity。即使是像requestWindowFeature(Window.FEATURE_NO_TITLE);这样的粗暴修补也只会导致应用程序崩溃。 - John

3

为什么要更改Android内置的主题

根据您的活动需求,您需要按照以下方式实施:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

根据@arianoo的说法,您必须使用此功能。我认为这是更好的隐藏标题栏主题的方法。

2
在您的styles.xml中,修改名为“AppTheme”的样式,如下所示:
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item> 
    </style>

1
在清单文件的应用程序标记中使用android:theme="@android:style/Theme.NoTitleBar"可删除整个应用程序的标题栏,或将其放置在活动标记中以从单个活动屏幕中删除标题栏。

这样做会不会也将主题修改为旧风格的? - Miguel

0
 this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);

可以这样写得更“正确”:requestWindowFeature(android.view.Window.FEATURE_NO_TITLE); - Reinherd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接