【问题标题】:How to add image instead of title text in android如何在android中添加图像而不是标题文本
【发布时间】:2014-10-27 09:25:20
【问题描述】:

我想添加这个图像而不是标题文本和操作栏的默认 android 图像,如上图所示。

【问题讨论】:

标签: android android-actionbar android-titlebar


【解决方案1】:

根据我对此问题的尝试,您可以通过制作一个充当操作栏的布局来做到这一点,然后在您拥有的每个活动中调用它并设置您的清单。在这里,我给你一些线索,以及如何在清单和你的活动类中设置它:

  1. 对您的“操作栏布局”进行编码,我们将其命名为 window_header.xml。把它放在你的 res>layout 目录中。只需制作一个包含图像视图的布局,将布局的高度设置为您的操作栏高度。 您还可以设置颜色和您想要的所有内容,只需使用您设置的高度使其像普通布局即可。

  2. 在 Manifest 中的 android:theme 部分,将其更改为如下所示:

android:theme="@style/CustomTheme"

  1. 要完成它,您需要一个资源文件,说明它将使用自定义样式,这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="CustomWindowTitleBackground">
            <item name="android:background">#323331</item>
        </style> 
        <style name="CustomTheme" parent="android:Theme">
            <item name="android:windowTitleSize">35dip</item>
            <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
        </style>
    </resources>
    
  2. 最后,在您的活动 OnCreate 方法中调用它,如下所示:

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_header);
    ImageView icon = (ImageView) findViewById(R.id.icon);
    this.icon.setImageResource(R.drawable.browse);
    

试试吧,一点点探索和自我创新会做得更好。希望对你有帮助

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多