代码之家  ›  专栏  ›  技术社区  ›  Casebash

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

  •  15
  • Casebash  · 技术社区  · 14 年前

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

    alt text http://lh3.ggpht.com/_KP55v0Zt2pk/S80CUbrYkCI/AAAAAAAAARY/FJ_WPt8ah2s/s800/title_bar.png

    有没有办法去掉灰色的边界?框架布局在 com.android.internal.R.id.title_container ,因此通过id访问帧是脆弱的。

    7 回复  |  直到 10 年前
        1
  •  16
  •   user486646    13 年前

    我一直在努力解决这个问题,现在我有了答案!

    正如您可能已经看到的,您需要创建一个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。与我在web上看到的这个文件的不同版本不同,我在文件中添加了一行,将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>
    
        2
  •  7
  •   Casebash    14 年前

    实际上不需要使用自定义布局来自定义背景图像。以下代码 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" .

        3
  •  1
  •   Donal Rafferty    14 年前

    在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
  •  1
  •   Chris Balavessov    12 年前

    我通过获取标题容器并将填充设置为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
  •  1
  •   DanKodi    12 年前

    如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>
    
        6
  •  0
  •   Birdnado    11 年前

    从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
  •  0
  •   Jasper    10 年前

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

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

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

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

    这为我解决了问题。