Android自定义TitleBar-隐藏文本

发布于 2024-10-05 01:28:58 字数 1475 浏览 7 评论 0原文

我在我的 Android 应用程序中使用带有背景图像的标题栏。

value/custom_styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="TitlebarBackgroundStyle">
        <item name="android:background">@drawable/titlebar</item>
    </style>
    <style name="Theme.MyCustomTheme" parent="android:Theme"> 
        <item name="android:windowTitleBackgroundStyle">@style/TitlebarBackgroundStyle</item>
        <item name="android:windowTitleSize">45dp</item>
    </style>
</resources>

layout/titlebar.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="45dip"
 android:gravity="center_vertical">
 <ImageView
  android:id="@+id/header"
  android:src="@drawable/titlebar"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
</LinearLayout>

Manifest:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/Theme.MyCustomTheme">

到目前为止效果很好。

我现在唯一想做的就是隐藏标题栏上的文本......

我尝试过:

<item name="android:textSize">0dp</item>

但没有运气。有什么方法可以设置透明度或其他一些属性,使我可以看到背景图像但看不到文本?

谢谢你的想法 罗恩

I am using a titlebar with a background image in my android app.

values/custom_styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="TitlebarBackgroundStyle">
        <item name="android:background">@drawable/titlebar</item>
    </style>
    <style name="Theme.MyCustomTheme" parent="android:Theme"> 
        <item name="android:windowTitleBackgroundStyle">@style/TitlebarBackgroundStyle</item>
        <item name="android:windowTitleSize">45dp</item>
    </style>
</resources>

layout/titlebar.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="45dip"
 android:gravity="center_vertical">
 <ImageView
  android:id="@+id/header"
  android:src="@drawable/titlebar"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
</LinearLayout>

Manifest:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/Theme.MyCustomTheme">

This works pretty well so far.

The only thing I'd like to do now is to hide the text on the titlebar ...

I tried:

<item name="android:textSize">0dp</item>

but with no luck. Is there any way to set the transparency or some other property that would allow me to see the background image but not the text?

thanks for your thoughts
Ron

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

陌伤ぢ 2024-10-12 01:28:59

您可以使用 Java 而不是资源 xml 来完成此操作,因为标题栏上的 TextView 是由 Android Java 源代码中的某些行以编程方式确定的。

您可以通过以下方式隐藏标题栏的 TextView:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
    root = (LinearLayout) decorView.getChildAt(0);
    FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
    TextView title = (TextView) titleContainer.getChildAt(0);
    title.setVisibility(View.GONE);
}

You can do it in Java, not resource xml, since the TextView on the title bar is determined programmatically by some lines in the Android Java source code.

You can hide the title bar's TextView in this way :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
    root = (LinearLayout) decorView.getChildAt(0);
    FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
    TextView title = (TextView) titleContainer.getChildAt(0);
    title.setVisibility(View.GONE);
}
帥小哥 2024-10-12 01:28:59

我不确定,但您也可以尝试在清单中声明没有标签的活动。

<activity android:name="...">

I'm not sure, but you can also try to declare your activities without label in manifest.

<activity android:name="...">
自由范儿 2024-10-12 01:28:59

将其添加到您的清单中

<application  android:label="bla bla"
              android:icon="@drawable/bla bla"
            --->  android:theme="@android:style/Theme.NoTitleBar">

Add this to your manifest

<application  android:label="bla bla"
              android:icon="@drawable/bla bla"
            --->  android:theme="@android:style/Theme.NoTitleBar">
南薇 2024-10-12 01:28:59

尝试使用空布局设置一个空的自定义标题栏。

Boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);    
    setContentView(R.layout.listviewoffers);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebarlayout);

Try to set an empty custom titlebar with an empty layout.

Boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);    
    setContentView(R.layout.listviewoffers);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebarlayout);
痕至 2024-10-12 01:28:59

幸运的是,方法太简单了(我猜你的标题确实有一个自定义布局)

如果你不知道怎么做,请看一下:如何在 Android 中创建自定义窗口标题

隐藏默认标题文本只需一行代码。只需在 onCreate() 中写入 setTitle(""); 即可。

就是这样!希望这有帮助!

Fortunately the way is too simple (I guess you do have a custom layout for your title)

If you don't know to do how please take a look: How to Create Custom Window Title in Android

Hiding the default title text is one line of code. Just write setTitle(""); inside onCreate().

That's it! Hope this helps!

层林尽染 2024-10-12 01:28:59

将您的主题设置为活动的自定义主题。主题如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme.Black.OptionalActionBar" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowActionBar">true</item>
        <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
    </style>

    <style name="MyActionBarStyle" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:displayOptions">showHome</item>
    </style>

    <style name="OptionalVisibility">
        <item name="android:visibility">invisible</item>
        <item name="android:height">1dp</item>
    </style>

</resources>

ActionBarStyle 覆盖操作栏的行为。请注意,我将样式设置为全息操作栏,然后将 displayOptions 更改为 showHome。这只会显示主页图标。您还可以将其设置为 showTitle ,这样只会显示文本。或者两者都 showHome|showTitle。如果将其留空, 那么文本和图标将一起消失。

注意:我知道这适用于 3.0 (api 11) 及更高版本...我不确定它是否适用于较低的 API 级别。

我知道这已经有很多年了......但为遇到此问题的其他人发布。

Set your theme to a custom theme for the activity. The theme would look like this:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme.Black.OptionalActionBar" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowActionBar">true</item>
        <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
    </style>

    <style name="MyActionBarStyle" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:displayOptions">showHome</item>
    </style>

    <style name="OptionalVisibility">
        <item name="android:visibility">invisible</item>
        <item name="android:height">1dp</item>
    </style>

</resources>

The ActionBarStyle overrides the behavior of the action bar. Notice I set the style to the holo actionbar and then change the displayOptions to showHome. This will only show the home icon. You can also set it to showTitle which will only show the text. Or both showHome|showTitle. If you leave it blank, <item name="android:displayOptions"></item> then the text and icon will go away all together.

NOTE: I know this will work for 3.0 (api 11) and above...I am not certain if it will work for lower API levels.

I know this is years old...but posting for others who encounter this problem.

弥繁 2024-10-12 01:28:59
android:theme="@android:style/Theme.NoTitleBar"> 

将其添加到您的清单文件中就可以了..

android:theme="@android:style/Theme.NoTitleBar"> 

adding this in your manifest file works..

一个人的旅程 2024-10-12 01:28:59
android:label=""

将其添加到清单中

android:label=""

add this to manifest

美男兮 2024-10-12 01:28:58

如果您正在使用 API 级别 11 或更高版本,则可以在您的 Activity 中执行此操作

final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);

或者您可以通过将自定义样式应用到您的 Activity 来完成此操作

  <style name="CustomWindowTitle" parent="android:TextAppearance.WindowTitle">
           <item name="android:visibility">gone</item>     
  </style>    

  <style name="MainTheme" parent="@android:Theme">    
     <item name="android:windowTitleStyle">
       @style/CustomWindowTitleText</item>   
 </style>

,例如 然后将其添加到您的 Activity 中,例如

 <activity
            ......
 android:theme="@style/MainTheme"/>

If you are working on API level 11 or above than do this in your activity

final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);

Or you can do this by applying custom style to your activity like

  <style name="CustomWindowTitle" parent="android:TextAppearance.WindowTitle">
           <item name="android:visibility">gone</item>     
  </style>    

  <style name="MainTheme" parent="@android:Theme">    
     <item name="android:windowTitleStyle">
       @style/CustomWindowTitleText</item>   
 </style>

And then add it to your activity like

 <activity
            ......
 android:theme="@style/MainTheme"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文