【问题标题】:android custom titlebar is working with some issuesandroid自定义标题栏正在处理一些问题
【发布时间】:2011-11-02 18:01:20
【问题描述】:

我正在使用以下 xml 文件制作自定义标题栏:

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/myTitle"
 android:text="custom title bar"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@drawable/background" />

在我的活动的 onCreate() 中,我有以下代码:

public class CustomTitleBar extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.main);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
    }
}

标题栏来了没问题。我设置为背景的图像(图像的名称也是背景,你可以在上面的xml中看到)也来了。但问题出在左右两侧图像还有一点间隙,即图像没有覆盖父项的整个宽度,尽管 layout_width 已设置为“fill_parent”。

任何想法。请帮助。

【问题讨论】:

    标签: android layout


    【解决方案1】:

    由于standard theme 中的默认windowTitleBackgroundStyle 使用9-patch drawablepadding,因此框架添加了左侧/右侧的小间隙。这是一个关于如何覆盖它的示例:

    在 AndroidManifest.xml 中,为您的 Activity 添加android:theme

    <activity
        android:theme="@style/MyCustomTitlebar"
        android:label="Custom Titlebar"
        android:name=".CustomTitlebar" />
    

    然后在资源中的某处定义自定义主题(例如在 res/values/themes.xml 中):

    <?xml version="1.0" encoding="UTF-8"?>
    <resources
        xmlns:android="http://schemas.android.com/apk/res/android">
        <style
            name="MyCustomTitlebar"
            parent="@android:style/Theme">
            <item
                name="android:windowTitleBackgroundStyle">@style/MyBackground</item>
        </style>
        <style
            name="MyBackground">
            <item
                name="android:background">@drawable/background</item>
        </style>
    </resources>
    

    由于我们将背景移动到样式中,我们可以将您的 mytitle.xml 布局修改为以下内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="custom title bar" />
    

    您可能需要调整您的背景(如果它是 9 补丁,则它有一些填充)或只是在 mytitle.xml 布局中设置填充(使用 paddingLeft/Right/Top/Bottom)。

    【讨论】:

      【解决方案2】:

      查看这篇文章,了解专为不同设备密度设计的自定义标头。 还使用样式和主题设计您的页眉。 Custom Header for Multiple Devices

      【讨论】:

      • 请检查您的链接是否仅为此问题的链接。
      • @AndroidKiller 请立即检查.. 发布的链接有误
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      相关资源
      最近更新 更多