使用AppCompatActivity进行android材料设计

Rob*_*hof 15 android android-appcompat material-design

为了支持API 19及更低版本,我让我的活动扩展AppCompatActivity.我尝试为v21设置以下父主题parent="android:Theme.Material" 当我尝试运行我的应用程序时,它给出了一个异常并告诉我使用Activity而不是AppCompatActivity.

这是否意味着我必须创建新的活动,扩展API 21及更高版本的活动以获得材料设计?或者,还有更好的方法?

Gab*_*tti 8

AppCompatActivity要求的程序兼容性主题.使用不同的主题,就像android:Theme.Material你会得到的那样

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
Run Code Online (Sandbox Code Playgroud)

只需在styles.xml文件中定义一个主题:

<style name="AppTheme" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

使用AppCompat主题,您可以在API <21的设备中使用Material设计.

android:Theme.Material一个可以仅与API> = 21使用.