27

我正在通过禁用标准之一并自己管理所有内容来为活动创建自定义标题。我想知道是否可以根据我的需要替换/主题标准标题。

我可以通过更改 windowXYZStyle 项目通过主题自定义大小、背景图像和文本。

我唯一找不到的东西 - 我如何添加图像而不是文本。我已经尝试requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) 并分配自定义布局 - 但它似乎不起作用。

编辑:这是测试建议的报告,代码如下 - 结果 - 图像视图未显示。

活动

public class SettingsActivity extends PreferenceActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

    }
}

XML :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:paddingLeft="5dip"
    android:background="@drawable/titlebar_bg"
    android:layout_gravity="left|center"
>
    <ImageView
        android:id="@+id/logo"
        android:src="@drawable/title_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
4

4 回答 4

36

可以设置自己的自定义标题布局,但执行顺序很重要。您必须按以下顺序执行操作:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.my_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_title);

此外,您可能需要增加标题的大小;如果你不这样做,那么你的自定义布局的底部可能会被你的 Activity 覆盖。您可以通过添加指定标题大小的主题来更改大小。这将进入一个值 XML 文件:

<resources>
    <style name="LargeTitleTheme">
        <item name="android:windowTitleSize">40dip</item>
    </style>
</resources>

然后,您需要在 AndroidManifest.xml 中为您的 Activity(或应用程序,如果您希望整个应用程序具有此自定义标题栏)设置主题:

<activity android:name=".MyCustomTitleActivity" android:theme="@style/LargeTitleTheme" />
于 2010-01-18T16:22:07.540 回答
7

你总是可以使用:

requestWindowFeature(Window.FEATURE_NO_TITLE);

然后在 Activity 的视图中创建自己的标题栏。

于 2010-01-18T15:31:24.130 回答
1

您确定需要在标题中使用图像,而不是将其作为背景图像的一部分并将标题留空吗?使用NinePatch图像,我希望您能够获得在任何设备上看起来都不错的结果?

于 2010-04-29T01:15:06.287 回答
1

我已使用以下代码将我的自定义标题适合屏幕

<ImageView
    android:id="@+id/header_img"
    android:src="@drawable/header_img"
    android:layout_width="700dip"
    android:layout_height="70dip" />
于 2015-03-14T18:24:10.803 回答