自定义标题栏没有填充(Android)

Cas*_*ash 15 user-interface android

所以我使用这个线程中的技术为我的标题栏使用自定义背景.不幸的是,框架将我的布局放在一个带有填充的FrameLayout(title_container)中,如下所示.

alt text http://lh3.ggpht.com/_KP55v0Zt2pk/S80CUbrYkCI/AAAAAAAAARY/FJ_WPt8ah2s/s800/title_bar.png

无论如何要删除灰色边框?帧布局是在中定义的com.android.internal.R.id.title_container,因此通过ID访问帧将是脆弱的.

小智 16

我正在努力解决同样的问题,现在我有了答案!

您可能已经看过几个地方,您需要创建一个themes.xml资源:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme" parent="android:Theme">
            <item name="android:windowTitleSize">44dip</item>
            <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

请注意,在视图,活动或应用程序级别中,您需要在自定义标题栏中选择此主题时,不会经常提及它.我通过在应用程序中添加android:theme属性在应用程序级别执行此操作:

  <application android:icon="@drawable/icon"
               android:label="@string/app_name"
               android:theme="@style/MyTheme">
Run Code Online (Sandbox Code Playgroud)

您还需要一个styles.xml来定义WindowTitleBackgroundStyle.不同于这个文件的各个版本,我已经看到浮动网络,我已经添加了一个额外的行,将填充设置为0,摆脱你抱怨的填充:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="WindowTitleBackground">
        <item name="android:background">@android:color/transparent</item>
        <item name="android:padding">0px</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)


Cas*_*ash 7

实际上不需要使用自定义布局来自定义背景图像.以下代码theme.xml将起作用:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="TitlebarBackgroundStyle">
        <item name="android:background">@drawable/header_bg</item>
    </style>
    <style name="Theme.OTPMain" parent="android:Theme"> 
        <item name="android:windowTitleBackgroundStyle">@style/TitlebarBackgroundStyle</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

但是,如果您确实想要使用自定义标题栏,则以下代码将删除边距:

ViewGroup v = (ViewGroup) findViewById(android.R.id.title);
v.setPadding(0, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)

title_bar_background被设置为XML中背景图像的ID.我设置android:scaleType="fitXY".