0
我已經使用AppcompatActivity並集成了android.support.v7.widget.Toolbar。但是,我第一次遇到一個未知的問題。我的工具欄顯示在xml預覽中,但運行應用程序後,工具欄消失了。這裏是我的代碼和樣式文件。這裏是三個文件MainActivity,佈局文件和值樣式文件夾'公共類MainActivity擴展AppCompatActivity {帶工具欄的AppcompatActivity在apprunning後不顯示工具欄,但在xml預覽中顯示android studio
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}`
和佈局文件,我刪除了我的代碼只是寫在工具欄部分爲了清楚起見,並且樣式文件Apptheme.base主題是/是由值-V21繼承了父主題style.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<!--<RelativeLayout
android:id="@+id/rl_toolbar_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/tv_home_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:drawablePadding="10dp"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>-->
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
in values/style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.base">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>
</style>
<style name="Button" parent="AppTheme.base">
<item name="android:textColor">#ffffff</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">@drawable/primary_round</item>
</style>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.base">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>
</style>