Android custom TitleBar - hide text

2019-07-28 19:30发布

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

9条回答
淡お忘
2楼-- · 2019-07-28 20:02

Add this to your manifest

<application  android:label="bla bla"
              android:icon="@drawable/bla bla"
            --->  android:theme="@android:style/Theme.NoTitleBar">
查看更多
再贱就再见
3楼-- · 2019-07-28 20:05

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);
}
查看更多
\"骚年 ilove
4楼-- · 2019-07-28 20:05
android:theme="@android:style/Theme.NoTitleBar"> 

adding this in your manifest file works..

查看更多
登录 后发表回答