Custom title bar without padding (Android)

后端 未结 7 625
失恋的感觉
失恋的感觉 2020-11-30 07:39

So I am using the techniques in this thread to use a custom background for my titlebar. Unfortunately the framework places my layout inside a FrameLayout (

相关标签:
7条回答
  • 2020-11-30 08:29

    As mentioned by dalewking, you can change the Manifest like this:

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

    But it did not work for me until I added

    android:theme="@style/MyTheme"
    

    to the activity it self like:

        <activity
            android:name=".MainActivity" 
            android:screenOrientation="portrait"
            android:theme="@style/CustomTheme" >            
        </activity>
    
    0 讨论(0)
  • 2020-11-30 08:30

    I was struggling with this same issue and now I have the answer!

    As you probably have seen several places, you need to create a themes.xml resource:

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

    Note it is not often mentioned in the sites talking about custom title bars you need to select this theme at the view, activity, or application level. I do it at the application level by adding android:theme property to the application:

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

    You also need a styles.xml to define that WindowTitleBackgroundStyle. Unlike the various versions of this file I have seen floating aroung the web, I have added one additional line to the file that sets the padding to 0 which gets rid of the padding you are complaining about:

    <?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>
    
    0 讨论(0)
  • 2020-11-30 08:30

    As of 4.2 and ADT 21 when creating a default layout standard dimensions are added to the parent container:

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

    Values are located in 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>
    

    Either remove them from the parent or change the dimens.xml values to your desired values.

    0 讨论(0)
  • 2020-11-30 08:37

    I got rid of the padding by getting the title container and setting the padding to 0. It works on 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 讨论(0)
  • 2020-11-30 08:40

    There is actually no need to use a custom layout to customise the background image. The following code in theme.xml will work:

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

    However, if you do actually want to use a custom title bar, then the following code will remove the margin:

    ViewGroup v = (ViewGroup) findViewById(android.R.id.title);
    v.setPadding(0, 0, 0, 0)
    

    title_bar_background was set as the ID of the background image in the XML. I set android:scaleType="fitXY".

    0 讨论(0)
  • 2020-11-30 08:43

    There is a lenghty thread over on anddev.org that may be able to help you out

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

    I have followed it and successfully created my own title bar which allows me to set the padding.

    Xml for my title bar:


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

    The above creates a small gray bar with text aligned to the right and left sides

    0 讨论(0)
提交回复
热议问题