【问题标题】:AppcompatActivity with toolbar not showing toolbar after apprunning but shows on xml preview android studio带有工具栏的 AppcompatActivity 在应用运行后不显示工具栏,但在 xml 预览 android studio 上显示
【发布时间】:2015-10-20 11:24:49
【问题描述】:

我已经使用过 AppcompatActivity 并集成了 android.support.v7.widget.Toolbar 。但这是我第一次遇到未知问题。我的工具栏显示在 xml 预览中,但运行应用程序后工具栏消失了。这是我的代码和样式文件。这里有三个文件 MainActivity ,布局文件和样式文件来自 values 文件夹`public class MainActivity extends AppCompatActivity {

    @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 主题是由 values-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>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    我也遇到了这个问题,我通过修复 Manifest 中的活动声明得到了解决。 删除主题行(如果存在):

    <activity
        ...
        android:theme="@style/AppTheme.NoActionBar"
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      相关资源
      最近更新 更多