【问题标题】:No Title Bar Android Theme没有标题栏 Android 主题
【发布时间】:2013-02-04 18:10:40
【问题描述】:

在我的应用程序中,我想使用 Theme.NoTitleBar,但另一方面我也不想松开 Android OS 的内部主题。我在网上搜索并找到了以下答案。我修改了我的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>

里面的值-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>

里面的值-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>

【问题讨论】:

  • 你不会因为没有设置标题栏而失去 Android 主题。您在 XML 布局中做错了什么。请发布布局,以便我们进行审查。这是否适用于所有 Android 平台版本?或者只是一个特定的
  • 它发生在所有平台上。我在红色文件夹中创建了布局文件夹,例如 layout-ldpi、layout-mdpi、layout-hdpi 和 layout-xhdpi,并且还具有包含不同屏幕的不同图像的可绘制文件夹。屏幕截图取自 Galaxy s3,但在模拟器上也是如此
  • 那么你能发布其中一种布局吗?除非您有一些特殊情况,否则您不需要基于 dpi 的不同布局。
  • @JaySoyer 我已经编辑了帖子,请看一下...
  • 好的。我的下一个问题是您是否使用 drawable-mdpi、drawable-hdpi 等。该 XML 专门用于哪种布局?

标签: android android-layout android-theme


【解决方案1】:

如果您希望保留 Ui 的原始样式并删除标题栏而不对此产生任何影响,则必须删除活动中的标题栏而不是清单。保留您在清单中的原始主题样式,并且在您希望没有标题栏的每个活动中使用 this.requestWindowFeature(Window.FEATURE_NO_TITLE);oncreate() 方法 before setcontentview() 像这样:

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

【讨论】:

  • 这个可行,但这不是最好的解决方案,标题栏在消失之前会闪烁几秒钟
  • 感谢它为我工作......投票
  • 我以这个确切的顺序使用了完全相同的行,但我得到了 excpetion:'android.util.AndroidRuntimeException: requestFeature() must be called before added content' - 有人知道为什么吗?
【解决方案2】:

要隐藏操作栏,请在值/样式中添加以下代码

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

【讨论】:

  • "android:windowActionBar" 需要 API +11 所以因为我的兼容性是 +8,所以 "android:windowNoTitle" 成功了谢谢 ;)
【解决方案3】:

在您的清单中使用:-

    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 的同一父级却没有。

【讨论】:

  • 意外惊喜,此页面上没有任何内容对我有用。我怀疑我将停止扩展ActionBarActivity,转而支持香草Activity。甚至像 requestWindowFeature(Window.FEATURE_NO_TITLE); 这样的野蛮混蛋也只是 FC。
【解决方案4】:

你为什么要改变android os内置的主题

根据您的活动要求您必须以这种方式实施

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

@arianoo 说你必须使用这个功能。

我认为这是隐藏标题栏主题的更好方法。

【讨论】:

    【解决方案5】:

    在你的styles.xml中,修改样式“AppTheme”

        <!-- Application theme. -->
        <style name="AppTheme" parent="AppBaseTheme">
            <item name="android:windowActionBar">false</item>
            <item name="android:windowNoTitle">true</item> 
        </style>
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

      • 这不会也将主题修改为旧样式吗?
      【解决方案7】:
       this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
      

      【讨论】:

      • 会像这样“更正确”:requestWindowFeature( android.view.Window.FEATURE_NO_TITLE );
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多