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

為了賬號安全,請及時綁定郵箱和手機立即綁定

如何動態設置Title中的文字?

我想動態的設置標題文字,在TopBar中寫個title.setText()方法,然后在Activity中調用用來動態設置文字,發現不管用,請問該如何做呢?http://img1.sycdn.imooc.com//56d900450001103106340215.jpg

正在回答

4 回答

public void setTitleText(String titleText){
? ?tvTitle.setText(titleText);
}

TopBar方法里寫上這個方法 ?然后在MainActivity 調用topBar.setTitleText("我是第一個界面");就改變了

0 回復 有任何疑惑可以回復我~
<?xml?version="1.0"?encoding="utf-8"?>
<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"
????xmlns:xiaoling="http://schemas.android.com/apk/res-auto"
????android:orientation="vertical"
????android:layout_width="match_parent"
????android:layout_height="match_parent">
????<com.custom.mycustomview.custom.View1
????????android:id="@+id/view1"
????????android:layout_width="match_parent"
????????android:layout_height="50dp"
????????xiaoling:barTitle="標題欄"
????????android:paddingLeft="10dp"
????????android:paddingRight="10dp"
????????xiaoling:leftImage="@mipmap/ic_nav_back_p"
????????xiaoling:rightImage="@mipmap/ic_map_location"
????????xiaoling:barTitleTextSize="@dimen/d18"
????????xiaoling:barTitleTextColor="@color/black"
????????/>

</LinearLayout>

上述xml文件名叫:include_topbar.xml

接下來我在要用的布局(假設名字叫activity_main)中引用這個xml:

<include?layout="@layout/include_topbar"/>

在引用activity_main布局的activity中只要聲明:

private View1 view1;

view1 = (View1) findViewById(R.id.view1);

你之前不是在自定義view中添加了設置標題的方法了嗎,這時候只要用view1.setTitle(假設setTitle是你寫的方法名)就可以設置具體想要的控件啦~

不想這么include,那就直接復制粘貼<com.custom.mycustomview.custom.View1這個控件及其相關設置

0 回復 有任何疑惑可以回復我~

需要在每個Fragment的布局文件中都添加進這個自定義控件,然后在每個Fragmen里單獨findViewById、設置值


0 回復 有任何疑惑可以回復我~

你寫的這個方法不在布局中調用,而是在Acitivity中調用:topBar.setTitleText("標題文字");

布局中如果要另起標題應該是:custom:title=“標題文字”;

0 回復 有任何疑惑可以回復我~
#1

NewOrin 提問者

我在布局文件中給title給定了值,可是當我要動態的改變title的值應該怎樣寫?比如說當我多個Fragment互相切換的時候,title跟著相應的變化
2016-03-05 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android UI模板設計
  • 參與學習       76032    人
  • 解答問題       233    個

快來學習如何在Android中自定義View,本次課程一定會讓你獲益匪淺

進入課程

如何動態設置Title中的文字?

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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