[英]Android - ActionBar Up button not visible when using TextView as ToolBar title
我在所有活動的頂部都添加了Toolbar
。 但是我希望標題位於中間,因此根據SO的一些建議,我在Toolbar
TextView
創建了一個TextView
,然后將其放在了中心。
現在,當我試圖加入這個指南的后續行動 增加一個后續行動| Android開發人員 ,我沒有得到任何UpButton。
這是因為在Toolbar
使用了TextView
嗎? 因為我在SO上讀到,工具欄基本上是一個View
並且可以像配置其他View
一樣進行配置,所以我不明白為什么這應該是一個問題。
If that is the reason, and I'd have to use the default
欄中If that is the reason, and I'd have to use the default
android:title =“ My Title” attribute in for the
,是否還有其他方法可以將其放在中間? 感謝您提供的任何幫助。
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);
}
}
您需要設置getSupportActionBar()。setHomeButtonEnabled(true); 啟用向上操作按鈕
Toolbar toolbar = (Toolbar) findViewById(R.id.lifeline_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.