2014-09-25 73 views
4

ActionBar - setTitle似乎不被調用。我使用ActionBar(比如HomeIsUp等)所做的其他所有事情都很順利,但setTitle並非如此。Android getActionBar()。setTitle不起作用

下面是一個簡單的活動代碼:

public class GuidelineDetails extends Activity implements OnClickListener { 

    String guideline_name; 
    String guideline_attachment; 

    public static TextView tv_title; 
    public static TextView tv_intro; 
    public static TextView tv_test; 
    public static WebView webview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.guideline_detail); 
     getActionBar().setTitle("Privet"); 
     getActionBar().setHomeButtonEnabled(true); 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setDisplayShowTitleEnabled(false); 
     getActionBar().setIcon(R.drawable.listbullet); 

///// some code 
} 
} 

爲何不設置標題?

在清單:

<activity 
      android:name=".GuidelineDetails" 
      android:configChanges="orientation" 
      android:label="Guideline" 
      android:screenOrientation="portrait" 
      android:theme="@android:style/Theme.Holo.Light" > 
     </activity> 
佈局

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <TableRow 
       android:id="@+id/tableRow1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

       <TextView 
        android:id="@+id/guideline_title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingBottom="1dp" 
        android:paddingLeft="10dp" 
        android:paddingRight="1dp" 
        android:paddingTop="35dp" 
        android:textSize="18dp" 
        android:textStyle="bold" /> 
      </TableRow> 

     </TableLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.16" 
     android:orientation="vertical" >  

     <TableRow 
       android:id="@+id/tableRow4" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

      <WebView 
    android:id="@+id/webviewD" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
/> 

     </TableRow> 



    </LinearLayout> 

</LinearLayout> 
+0

那麼你想要設置什麼標題「指南」或「女貞」?因此,在清單中或通過'onCreate'中的代碼將**設置爲**。所以決定和做一個 – 2014-09-25 18:31:04

+0

@ShobhitPuri刪除語句android:label =「Guideline」,但仍然不起作用:( – 2014-09-25 18:45:45

+1

是的,因爲你有'setDisplayShowTitleEnabled(false)'。試試讓它成真 – 2014-09-25 18:48:27

回答

19

getActionBar().setDisplayShowTitleEnabled(false); 

改變其活動,以

getActionBar().setDisplayShowTitleEnabled(true); 
+0

:D明顯。謝謝! – 2014-09-25 18:54:41

+3

不適用於我:getSupportActionBar()。setDisplayShowTitleEnabled(true); getSupportActionBar()。setTitle(R.string。 action_bar_title); – 2016-12-16 18:13:01

0

你試圖設置字幕?因爲在某些情況下,如果設備屏幕在ActionBar中很小,您只能看到ActionBar圖標。試試這個.getActionBar()。setTitle(「Privet」);希望對你有幫助!

+1

1)你的代碼沒有任何區別我的。 2)嘗試setSubitle - 不起作用。 – 2014-09-25 18:47:32

0

我認爲這是因爲Manifest文件超出了規則。我與我的圖標有同樣的問題,所以我不得不在清單文件(android:label="Guideline")中更改它,或者如果您必須在語法上設置它,請不要在Manifest中設置標籤。

+1

感謝您的建議,但它並沒有幫助:(從清單中刪除這個 – 2014-09-25 18:46:35

0

儘量延長ActionBarActivity,而不是因爲

0

雖然這解決了,我加入了這個答案的人誰也有這個問題。確保你的最低API是11.

{Rich}