【问题标题】:Custom title bar without padding (Android)没有填充的自定义标题栏(Android)
【发布时间】:2025-12-09 05:20:12
【问题描述】:

所以我使用this thread 中的技术为我的标题栏使用自定义背景。不幸的是,该框架将我的布局放置在 FrameLayout (title_container) 中,它具有如下所示的填充。


(来源:ggpht.com

有什么办法可以去掉灰色边框吗?框架布局在com.android.internal.R.id.title_container 中定义,因此通过ID 访问框架会很脆弱。

【问题讨论】:

    标签: android user-interface


    【解决方案1】:

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

    你可能已经看到好几个地方了,你需要创建一个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。与我在网上看到的该文件的各种版本不同,我在文件中添加了一行额外的行,将填充设置为 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>
    

    【讨论】:

    • ViewGroup 上的 setPadding 并没有为我做这件事。您建议使用 0 填充设置背景。
    • 这个解决方案使我所有的填充都归零,即使是我明确声明的那些
    【解决方案2】:

    其实不需要使用自定义布局来自定义背景图片。 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 作为父母
    【解决方案3】:

    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>
    

    上面创建了一个灰色的小条,文本左右对齐

    【讨论】:

    • 这实际上对我不起作用。我正在尝试删除填充,而不是添加额外的填充
    【解决方案4】:

    我通过获取标题容器并将填充设置为 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);
    

    【讨论】:

      【解决方案5】:

      正如 dalewking 所说,您可以像这样更改 Manifest:

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

      【讨论】:

        【解决方案6】:

        从 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 值更改为您想要的值。

        【讨论】:

          【解决方案7】:

          我在styles.xml中为AppTheme添加了以下填充项

          <style name="AppTheme" parent="AppBaseTheme">
          ...
          <item name="android:padding">0dp</item>
          ...
          </style>
          

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

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

          这为我解决了。

          【讨论】:

            最近更新 更多