用形象自定义标题 [英] Custom title with image

查看:162
本文介绍了用形象自定义标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被禁用的标准之一,管理都要亲力亲为创建自定义标题的活动。我不知道是否有可能取代/主题非标准标题我的需求。

我可以通过改变windowXYZStyle项定制大小,背景图像,并经由主题文本

我唯一没找到 - 我怎么可以添加文本的形象代替。 我试过 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) 并指定自定义布局 - 但它似乎并不工作。

编辑:下面是测试的建议的报告,code如下 - 结果 - 图像视图没有显示出来。

活动

 公共类SettingsActivity扩展preferenceActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml.settings);

    }
}

XML:

<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /标题
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =26dip
    机器人:以下属性来=5dip
    机器人:背景=@可绘制/ titlebar_bg
    机器人:layout_gravity =左|中心
>
    < ImageView的
        机器人:ID =@ + ID /标志
        机器人:SRC =@可绘制/ title_logo
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

< / LinearLayout中>
 

解决方案

这是可以设置自己的自定义标题布局,执行事务但是顺序。你必须按顺序做的事情:

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

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

 <资源>
    <样式名称=LargeTitleTheme>
        <项目名称=机器人:windowTitleSize> 40dip< /项目>
    < /风格>
< /资源>
 

然后你需要设置主题为您的活动(或应用程序,如果你想整个应用程序有这个自定义标题栏)在AndroidManifest.xml中:

 <活动机器人:名称=机器人MyCustomTitleActivity。:主题=@风格/ LargeTitleTheme/>
 

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>

解决方案

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" />

这篇关于用形象自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆