从片段设置自定义动作条标题 [英] Setting Custom ActionBar Title from Fragment

查看:127
本文介绍了从片段设置自定义动作条标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主 FragmentActivity ,我安装我的自定义动作条标题是这样的:

  LayoutInflater充气=(LayoutInflater)本
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    视图V = inflator.inflate(R.layout.custom_titlebar,NULL);

    TextView的电视=(TextView中)v.findViewById(R.id.title);
    字样TF = Typeface.createFromAsset(this.getAssets()
            字体/ capsuula.ttf);
    tv.setTypeface(TF);
    tv.setText(this.getTitle());

    actionBar.setCustomView(五);
 

这完美的作品。但是,一旦我打开其他片段,我想让标题发生改变。我不知道如何进入主活动做到这一点?在过去,我这样做:

 ((MainFragmentActivity)getActivity())。getSupportActionBar()的setTitle(
            catTitle);
 

有人可以提供意见的正确方法?

XML:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@机器人:彩色/透明>

    <的TextView
        机器人:ID =@ + ID /标题
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerVertical =真
        机器人:layout_marginLeft =5DP
        机器人:ellipsize =结束
        机器人:MAXLINES =1
        机器人:文本=
        机器人:文字颜色=#FFF
        机器人:TEXTSIZE =25sp/>

< / RelativeLayout的>
 

解决方案

你在做什么是正确的。 片段没有访问动作条的API,所以你要叫 getActivity 。除非你的片段是一个静态内部类,在这种情况下,你应该创建一个的WeakReference 的家长和呼叫活动。 getActionBar 从那里。

要设置为你的动作条,同时采用了自定义布局,在你的片段您需要的标题叫 getActivity()的setTitle(YOUR_TITLE)

您拨打的原因的setTitle 是因为我们在调用的getTitle 作为<$ C $称号C>动作条。 的getTitle 返回该标题活动

如果你不想让呼叫的getTitle ,那么你需要创建一个设置你的的TextView文本的公共方法活动承载片段

在活动

 公共无效setActionBarTitle(字符串名称){
    YOUR_CUSTOM_ACTION_BAR_TITLE.setText(职称);
}
 

在片段

 ((MainFragmentActivity)getActivity())setActionBarTitle(YOUR_TITLE)。
 

文档

<一个href="https://developer.android.com/reference/android/app/Activity.html#getTitle%28%29"><$c$c>Activity.getTitle

<一个href="https://developer.android.com/reference/android/app/Activity.html#getTitle%28%29"><$c$c>Activity.setTitle

另外,你不需要 this.whatever 在code您提供打电话,只是一个提示。

In my Main FragmentActivity, I setup my custom ActionBar title like this:

    LayoutInflater inflator = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.custom_titlebar, null);

    TextView tv = (TextView) v.findViewById(R.id.title);
    Typeface tf = Typeface.createFromAsset(this.getAssets(),
            "fonts/capsuula.ttf");
    tv.setTypeface(tf);
    tv.setText(this.getTitle());

    actionBar.setCustomView(v);

This works perfect. However, once I open other Fragments, I want the title to change. I am not sure how to access the Main Activity to do this? In the past, I did this:

((MainFragmentActivity) getActivity()).getSupportActionBar().setTitle(
            catTitle);

Can someone advise on the proper method?

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text=""
        android:textColor="#fff"
        android:textSize="25sp" />

</RelativeLayout>

解决方案

What you're doing is correct. Fragments don't have access to the ActionBar APIs, so you have to call getActivity. Unless your Fragment is a static inner class, in which case you should create a WeakReference to the parent and call Activity.getActionBar from there.

To set the title for your ActionBar, while using a custom layout, in your Fragment you'll need to call getActivity().setTitle(YOUR_TITLE).

The reason you call setTitle is because you're calling getTitle as the title of your ActionBar. getTitle returns the title for that Activity.

If you don't want to get call getTitle, then you'll need to create a public method that sets the text of your TextView in the Activity that hosts the Fragment.

In your Activity:

public void setActionBarTitle(String title){
    YOUR_CUSTOM_ACTION_BAR_TITLE.setText(title);
}

In your Fragment:

((MainFragmentActivity) getActivity()).setActionBarTitle(YOUR_TITLE);

Docs

Activity.getTitle

Activity.setTitle

Also, you don't need to call this.whatever in the code you provided, just a tip.

这篇关于从片段设置自定义动作条标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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