带图像的自定义标题

发布于 2024-08-18 14:44:12 字数 1223 浏览 3 评论 0原文

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

我可以通过更改 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>

i'm creating custom title for activity by disabling standard one and managing everything myself. I wonder if it's possible to replace/theme standart title to my needs.

I can customize size, background image, and text via themes by changing windowXYZStyle items.

The only thing i couldn't find - how i can add image instead of text.
I've tried requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)
and assign custom layout - but it doesn't seems to work.

EDIT : Here is a report of testing suggestions, code is below - result - image view is not showing up.

Activity

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天的雪花 2024-08-25 14:44:12

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

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

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

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

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

<activity android:name=".MyCustomTitleActivity" android:theme="@style/LargeTitleTheme" />

It's possible to set your own custom title layout, however the order of execution matters. You must do things in this order:

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

Additionally, you may need to increase the size of the title; if you don't, then the bottom of your custom layout may just be covered up by your Activity. You can change the size by adding a theme that specifies the title's size. This would go into a values XML file:

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

Then you'd need to set the theme for your Activity (or Application, if you want the entire application to have this custom title bar) in AndroidManifest.xml:

<activity android:name=".MyCustomTitleActivity" android:theme="@style/LargeTitleTheme" />
恍梦境° 2024-08-25 14:44:12

您始终可以使用:

requestWindowFeature(Window.FEATURE_NO_TITLE);

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

You could always use:

requestWindowFeature(Window.FEATURE_NO_TITLE);

And then create your own title bar inside your Activity's View.

红ご颜醉 2024-08-25 14:44:12

您确定需要在标题中使用图像,而不是仅仅将其组合为背景图像的一部分并将标题留空吗?使用 NinePatch 图像我希望你能够实现无论设备如何,结果都会很好看?

Are you sure that you need to use an image in the title rather than just combining it as part of your background image and leaving the title blank? Using a NinePatch image I would expect that you would be able to achieve a result that would look good on regardless of the device?

风透绣罗衣 2024-08-25 14:44:12

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

<ImageView
    android:id="@+id/header_img"
    android:src="@drawable/header_img"
    android:layout_width="700dip"
    android:layout_height="70dip" />

I have used the following code to fit my custom title to the screen

<ImageView
    android:id="@+id/header_img"
    android:src="@drawable/header_img"
    android:layout_width="700dip"
    android:layout_height="70dip" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文