亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從片段設置自定義ActionBar標題

從片段設置自定義ActionBar標題

一只甜甜圈 2019-12-25 14:33:59
在我的Main中FragmentActivity,我ActionBar像這樣設置我的自定義標題:    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);這完美。但是,一旦我打開other Fragments,我想更改標題。我不確定如何訪問Main Activity來執行此操作?過去,我這樣做:((MainFragmentActivity) getActivity()).getSupportActionBar().setTitle(            catTitle);有人可以建議適當的方法嗎?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>
查看完整描述

3 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

你在做什么是對的。Fragments沒有訪問ActionBarAPI的權限,因此您必須致電getActivity。除非您Fragment是靜態內部類,否則您應該WeakReference為父級創建一個并調用Activity。getActionBar從那里。


要ActionBar在使用自定義布局時為您設置標題,您Fragment需要致電getActivity().setTitle(YOUR_TITLE)。


你打電話的原因setTitle是因為你打電話getTitle為你的標題ActionBar。getTitle返回該標題Activity。


如果你不想讓通話getTitle,那么你就需要創建一個公共方法,設置你的文字TextView中Activity承載Fragment。


在您的活動中:


public void setActionBarTitle(String title){

    YOUR_CUSTOM_ACTION_BAR_TITLE.setText(title);

}

在您的片段中:


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

文件:


Activity.getTitle


Activity.setTitle


另外,您無需調用this.whatever提供的代碼,只需提示。


查看完整回答
反對 回復 2019-12-25
  • 3 回答
  • 0 關注
  • 660 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號