没有填充的自定义标题栏(Android) [英] Custom title bar without padding (Android)

查看:33
本文介绍了没有填充的自定义标题栏(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用
(来源:ggpht.com)

有没有办法去除灰色边框?框架布局在 com.android.internal.R.id.title_container 中定义,因此通过 ID 访问框架会很脆弱.

解决方案

我一直在为同样的问题苦苦挣扎,现在我有了答案!

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

请注意,在讨论自定义标题栏的网站中并不经常提到您需要在视图、活动或应用程序级别选择此主题.我在应用程序级别通过向应用程序添加 android:theme 属性来做到这一点:

 

您还需要一个styles.xml 来定义WindowTitleBackgroundStyle.与我在网上看到的该文件的各种版本不同,我在文件中添加了一行,将填充设置为 0,从而消除了您抱怨的填充:

So I am using the techniques in this thread to use a custom background for my titlebar. Unfortunately the framework places my layout inside a FrameLayout (title_container) which has padding as seen below.


(source: ggpht.com)

Is there anyway to remove the grey borders? The frame layout is defined in com.android.internal.R.id.title_container, so accessing the frame by ID would be fragile.

解决方案

I was struggling with this same issue and now I have the answer!

As you probably have seen several places, you need to create a themes.xml resource:

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

Note it is not often mentioned in the sites talking about custom title bars you need to select this theme at the view, activity, or application level. I do it at the application level by adding android:theme property to the application:

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

You also need a styles.xml to define that WindowTitleBackgroundStyle. Unlike the various versions of this file I have seen floating aroung the web, I have added one additional line to the file that sets the padding to 0 which gets rid of the padding you are complaining about:

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

这篇关于没有填充的自定义标题栏(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆