我曾经为这个问题苦苦挣扎,现在我终于找到了答案!
正如你可能在多个地方看到的那样,你需要创建一个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>
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/MyTheme">
<?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>
实际上,不需要使用自定义布局来自定义背景图像。以下代码在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"
。
在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>
以上代码创建了一个小的灰色条,文本分别对齐于右侧和左侧
正如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>
我通过获取标题容器并将填充设置为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);
<style name="AppTheme" parent="AppBaseTheme">
...
<item name="android:padding">0dp</item>
...
</style>
应用程序清单使用该主题:
<application
...
android:theme="@style/AppTheme" >
...
这对我解决了问题。
从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的值更改为您所需的值。