2016-12-08 159 views
-4

我是Android新手。我想在活動的頂部放置一個標題,類似於iOS中的UINavigationBar。我需要在標題左側放置一個圖標和一個標題(標題爲文字或圖像&標題)。請找到圖片鏈接,以瞭解我想要的標題。活動中的標題

https://www.dropbox.com/s/onsc3klzc0bafia/Screen%20Shot%202016-12-08%20at%201.00.13%20PM.png?dl=0

+0

訪問此鏈接的左上角添加箭頭:http://stackoverflow.com/questions/40960334/action-bar-with-後退箭頭/ 40960576#40960576 –

+0

您可以使用'ToolBar'作爲標題,並且如果您想使用'Drawable'圖標。在Android Studio中:轉到「File/New/Vector Asset/Icon」。點擊圖標並選擇需要的,然後使用。使用require圖標而不是後退圖標。 –

回答

3

用戶這在XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="@color/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<FrameLayout 
    android:id="@+id/frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</LinearLayout> 

,然後在活動的onCreate();添加此

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_toolbar); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    toolbar.setTitle("YOUR_TITLE"); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
} 

這條線將在工具欄

getSupportActionBar().setDisplayHomeAsUpEnabled(true);