2016-10-19 18 views
2

當動作條上按鈕不可見我對我所有的活動的頂部增加了一個Toolbar。但我希望標題在中心位置,所以根據SO的建議,我在Toolbar內創建了一個TextView,我能夠將它放在中心位置。
現在,當我試圖添加後續行動本指南Adding an Up Action | Android Developers下,我沒有得到任何UpButton。
這是因爲在Toolbar裏面使用了TextView?因爲我讀到SO上的工具欄基本上是View,可以像其他任何View一樣配置,所以我不明白爲什麼這應該是一個問題。Android的 - 使用的TextView作爲工具欄標題

  1. If that is the reason, and I'd have to use the default機器人:標題=「我的標題」 attribute in for the Toolbar`,是否有任何其他的方式把它的中心?
  2. 這個問題的解決方法是什麼,以便我可以得到一個Up Action以及中心的工具欄標題?你會

感謝您的幫助能夠提供。

XML佈局

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_width="match_parent"> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/lifeline_toolbar" 
     android:elevation="4dp" 
     android:background="@android:color/background_dark" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize"> 
     <TextView 
      android:layout_gravity="center_horizontal" 
      android:text="Lifeline" 
      android:textStyle="bold" 
      android:textSize="18sp" 
      android:id="@+id/lifeline_toolbar_title" 
      android:textColor="@android:color/white" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </android.support.v7.widget.Toolbar> 
    <LinearLayout> 
    </LinearLayout> 
    <LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

Java代碼的

public class ll_home extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_ll_home); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.lifeline_toolbar); 
     setSupportActionBar(toolbar); 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
    } 
} 
+0

@ M.WaqasPervez不,這沒有任何區別。仍然不行動按鈕。 – Yankee

+0

經過進一步閱讀,我認爲你對支持行動欄是正確的。因爲我認爲這是不正確的,所以我刪除了我的答案。 –

+1

現在我對此感到好奇,但我不在可以試驗的電腦上。想到一個問題:你是否在AndroidManifest.xml中指定了一個父活動? –

回答

0

您需要設置getSupportActionBar()setHomeButtonEnabled(真)。啓用上行動按鈕

Toolbar toolbar = (Toolbar) findViewById(R.id.lifeline_toolbar); 
setSupportActionBar(toolbar); 
ActionBar actionBar = getSupportActionBar(); 
actionBar.setDisplayHomeAsUpEnabled(true); 
actionBar.setHomeButtonEnabled(true); 
+0

謝謝Sunil,但那不起作用。 – Yankee