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

15

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

alt text
(来源:ggpht.com)

有没有办法去掉灰色边框?FrameLayoutcom.android.internal.R.id.title_container中定义,因此通过ID访问该帧可能不可靠。

7个回答

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>

请注意,在讨论自定义标题栏的网站中往往没有提到您需要在视图、活动或应用程序级别选择此主题。我通过将android:theme属性添加到应用程序来在应用程序级别上执行此操作:
  <application android:icon="@drawable/icon"
               android:label="@string/app_name"
               android:theme="@style/MyTheme">

你还需要一个styles.xml文件来定义WindowTitleBackgroundStyle。与我在网上看到的各种版本不同,我在文件中添加了一行额外的代码,将padding设置为0,这样就可以消除你所抱怨的padding:
<?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>

在ViewGroup上设置setPadding对我没有起作用。您建议使用0填充设置背景,这样做就可以了。 - AlanKley
这个解决方案会将所有填充值都变为零,即使我已经显式声明了一些填充值。 - Sourav Kanta

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>

然而,如果你想使用自定义标题栏,以下代码将移除边距:
ViewGroup v = (ViewGroup) findViewById(android.R.id.title);
v.setPadding(0, 0, 0, 0)

title_bar_background 在 XML 中被设置为背景图片的 ID。我设置了 android:scaleType="fitXY"


它似乎在Android 4上无法工作。你有任何想法为什么会这样吗?我正在使用android:Theme.Holo.Light作为父级。 - nLL

1

在anddev.org上有一个漫长的线程,可能能够帮助你解决问题。

http://www.anddev.org/my_own_titlebar_backbutton_like_on_the_iphone-t4591.html

我已经按照它的指引成功地创建了自己的标题栏,使我可以设置填充。

我的标题栏Xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="38px" android:layout_width="fill_parent"
android:background="@drawable/gradient">

<TextView android:id="@+id/title" android:layout_width="wrap_content"
android:gravity="center_vertical" style="@style/PhoneText"
android:layout_alignParentLeft="true"
android:text="New Title" android:background="@android:color/transparent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:padding="5dip" android:layout_marginBottom="7px"/>

<TextView android:id="@+id/time" android:layout_width="wrap_content"
android:gravity="center_vertical" style="@style/PhoneText2"
android:layout_alignParentRight="true"
android:text="Test Text" android:background="@android:color/transparent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:padding="4dip" android:layout_marginBottom="7px"/>
</RelativeLayout>

以上代码创建了一个小的灰色条,文本分别对齐于右侧和左侧


那对我实际上不起作用。我正在尝试去除填充,而不是添加额外的填充。 - Casebash

1

正如dalewking所提到的,您可以像这样更改清单:

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

但是直到我添加了它,它才对我起作用。

android:theme="@style/MyTheme"

到达活动本身,例如:

    <activity
        android:name=".MainActivity" 
        android:screenOrientation="portrait"
        android:theme="@style/CustomTheme" >            
    </activity>

1

我通过获取标题容器并将填充设置为0来消除了填充。它在Android 4上有效。

titleContainerId = (Integer)Class.forName("com.android.internal.R$id").getField("title_container").get(null);

ViewGroup vg = ((ViewGroup) getWindow().findViewById(titleContainerId));
vg.setPadding(0, 0, 0, 0);

0
我在styles.xml文件中的AppTheme主题中添加了以下填充项。
<style name="AppTheme" parent="AppBaseTheme">
...
<item name="android:padding">0dp</item>
...
</style>

应用程序清单使用该主题:

<application 
...
android:theme="@style/AppTheme" >
...

这对我解决了问题。


0

从4.2和ADT 21开始,在创建默认布局时,标准尺寸将添加到父容器中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

</RelativeLayout>

数值位于res/dimens.xml文件中:

<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

</resources>

要么将它们从父级中移除,要么将dimens.xml的值更改为您所需的值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接