Android无法删除自定义标题栏周围的填充

时间:2011-06-02 01:45:33

标签: android android-layout

我在标题标题中添加了一个自定义img,但无论我做什么,我在img的每一侧仍然有一个小间隙(也在此question中显示)

这是我的strings.xml文件中的xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello"></string>
    <string name="app_name"></string>
    <style name="LargeTitleTheme" parent="android:Theme.Light">
        <item name="android:windowTitleSize">44dip</item>
    </style>
</resources>

这是我活动中的代码(忽略了slop - 此时绝望的编码)

ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
    LinearLayout root = (LinearLayout) decorView.getChildAt(0);
    FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
    TextView title = (TextView) titleContainer.getChildAt(0);
    title.setGravity(Gravity.CENTER);
    Drawable drawable = getResources().getDrawable(R.drawable.nav);
    drawable.setBounds(0,0,0,0);
    title.setBackgroundDrawable(drawable);
    title.setPadding(0,0,0,0);
    title.setIncludeFontPadding(false);

4 个答案:

答案 0 :(得分:3)

您可以通过应用自定义主题(例如

)来覆盖默认填充
<style name="customTheme" parent="android:Theme">
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleStyle</item>
</style>

 <style name="WindowTitleStyle">
    <item name="android:padding">0px</item>
</style>

这将从自定义标题栏中删除默认填充。

答案 1 :(得分:2)

事实证明我必须修改标题容器本身的背景颜色:)

private void setNavigationAndTitle() {
        setTitle("Random Title");
        ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
        LinearLayout root = (LinearLayout) decorView.getChildAt(0);
        FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
        titleContainer.setBackgroundColor(Color.BLUE);
        TextView title = (TextView) titleContainer.getChildAt(0);
        title.setTextSize(20);
        title.setGravity(Gravity.CENTER);
        title.setBackgroundColor(Color.BLUE);
}

答案 2 :(得分: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值更改为所需的值。

答案 3 :(得分:-1)

您可以尝试将setPadding值设为负数