【问题标题】:Custom title bar not filling the screen自定义标题栏不填满屏幕
【发布时间】:2014-03-07 07:39:41
【问题描述】:

我的应用有一个自定义标题栏。



我编辑了 values/styles.xml 以使我的标题栏为 45dp。 (下)

<style name="AppBaseTheme" parent="android:Theme.Light">

    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->

</style>


<style name="CustomTheme" parent="AppTheme">
    <item name="android:windowTitleSize">45dp</item>
</style>



这是我的自定义标题栏布局(下)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/background_gradient"
android:gravity="center_vertical" >

<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="7dp"
    android:src="@drawable/info_icon"
    android:layout_alignParentLeft="true" >
</ImageView>

<ImageView
    android:layout_height="wrap_content"
    android:paddingRight="7dp"
    android:layout_width="wrap_content"
    android:src="@drawable/info_icon"
    android:layout_alignParentRight="true"
>

</ImageView>

</RelativeLayout>



这是我为上面的 RelativeView 设置的背景颜色的可绘制对象

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

<gradient
    android:type="linear"
    android:centerX="49%"
    android:startColor="#FFFF6417"
    android:centerColor="#FFffffff"
    android:endColor="#FFFF7308"
    android:angle="315"/>

</shape>



Android 清单文件:

android:theme="@style/CustomTheme"

在每个活动中我都这样做

super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.notice_main);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);

我相信我所做的一切都是正确的。

但我得到了这个。我不知道为什么。

【问题讨论】:

    标签: android android-titlebar


    【解决方案1】:

    您需要将活动设为全屏,即没有默认标题栏,然后设置自定义标题栏。通过这样做,灰色部分将被删除

     requestWindowFeature(Window.FEATURE_NO_TITLE);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    
        setContentView(R.layout.notice_main);
    
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
    

    在此之后,您可以正确设置左右边距的填充

    【讨论】:

    • 我完全按照你写的做了。我得到 android.util.AndroidRuntimeException: You cannot combine custom title with other title features
    • 仅使用 framlayout 将其放入布局中
    • 我将 relativelayout 放在 framelayout 中。 Framelayout 设置为填充父宽度和高度。我现在没有异常,但 UI 静止图像看起来像上图。
    【解决方案2】:

    试试这个作为自定义标题栏的布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" 
        android:background="@drawable/background_gradient">
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/info_icon"
            android:src="@drawable/info_icon" >
        </ImageView>
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/info_icon" >
        </ImageView>
    
    </LinearLayout>
    

    【讨论】:

    • @Brontok...抱歉回复晚了我必须为客户端演示准备应用程序...无论如何我粘贴了您的代码但我仍然​​得到灰色条......我也注意到屏幕周围到处都是灰色条带....imgur.com/kunnhJI
    【解决方案3】:

    最好在 style.xml 的 values 中创建以下代码

    <style name="WindowTitleBackground">     
        <item name="android:background">@drawable/titlebar_backgrnd</item>   
    </style>
    

    以及theme.xml中的以下代码在values中

    <style name="customTheme" parent="android:Theme">
        <item name="android:windowTitleSize">30dip</item> 
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 2021-08-19
      • 2016-05-16
      • 2020-07-20
      相关资源
      最近更新 更多