将应用程序主题设置为材质设计并使用材质按钮后,看不到布局预览

时间:2019-06-19 06:46:50

标签: java android android-layout material-design android-design-library

我将材质设计用作我的应用程序主题,但是由于这个原因,在android studio中查看布局预览时出现了一些问题。没有显示预览,但是当我运行代码时,我可以在设备上查看设计。

问题主要是因为我使用了材质按钮。当我注释按钮代码时,将显示预览,但是当我取消注释代码并刷新布局时,将再次不显示预览。

下面是我将材质主题应用于我的应用程序的代码:

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

下面是我在布局中使用的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
        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_width="match_parent"
        android:layout_height="match_parent">


    <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_marginTop="115dp"
            android:layout_centerHorizontal="true"
            android:textSize="30dp"
            style="@style/text_sui_generis"
            android:textColor="@color/colorPrimary"/>

    <RelativeLayout
            android:id="@+id/rlSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tvTitle"
            android:layout_marginTop="80dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/border_shadow"
            android:padding="5dp">

        <ImageView android:layout_width="20dp"
                   android:layout_height="20dp"
                   android:src="@drawable/ic_location"
                   android:id="@+id/locationImage"
                   android:layout_centerVertical="true"/>

        <Spinner
                android:id="@+id/locationSpinner"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_toEndOf="@id/locationImage"
                android:layout_marginStart="5dp"
                android:layout_centerVertical="true"/>

    </RelativeLayout>

    <com.google.android.material.button.MaterialButton
            android:id="@+id/signin_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sign_in"
            style="@style/buttonCustomFont"
            android:layout_below="@id/rlSpinner"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="60dp"
            android:elevation="1dp"/>

</RelativeLayout> 

3 个答案:

答案 0 :(得分:1)

您没有以我认为的风格写过buttonCustomFont

删除此行并刷新预览

 style="@style/buttonCustomFont"

完整代码

<com.google.android.material.button.MaterialButton
        android:id="@+id/signin_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sign_in"
        android:layout_below="@id/rlSpinner"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:elevation="1dp"/>

答案 1 :(得分:0)

在设计预览中使用“强制刷新布局”选项

enter image description here

答案 2 :(得分:0)

实际上,这是Android Studio的问题。

我在Android Studio 3.4.2稳定版中也遇到了此问题。在此稳定版本中,将App主题设置为“ Theme.MaterialComponents。*”

时,设计预览无法正常工作。

设计预览版可以与Beta版“ Android Studio 3.5 RC 1”一起正常使用

尝试下载最新的Beta版本。