首页 > 解决方案 > 迁移到 androidx 后未显示 android XML 预览

问题描述

我正在开发一个项目,我最近使用重构将整个项目更新为 androidx > 迁移到 andoidx,但现在它没有显示 XML 设计预览,只是说“等待构建完成”,尽管我构建 apk 并没有任何错误检查但应用程序运行良好只是预览不显示。

以下是 build.gradle 的代码


android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "abc.example.xyz"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'


}

主要活动java


import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;


public class Home extends AppCompatActivity {
    CardView tuts,prog,iq,quiz;
   // private InterstitialAd mInterstitialAd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Toolbar toolbar=findViewById(R.id.toolbar);
        toolbar.inflateMenu(R.menu.menu_video);
        setSupportActionBar(toolbar);


      tuts=(CardView) findViewById(R.id.Cview_tuts);
      prog=(CardView) findViewById(R.id.cView_prog);
      iq=(CardView) findViewById(R.id.Cview_IQ);
      quiz=(CardView) findViewById(R.id.cView_quiz);

/*
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());*/


        prog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Home.this,TutsActivity.class);
                startActivity(intent);
               // adload();
            }
        });
        tuts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Home.this,ProActivity.class);
                startActivity(intent);
               // adload();

            }
        });

        iq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Home.this,IqActivity.class);
                startActivity(intent);
               // adload();


            }
        });
        quiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Home.this,MainActivity.class);
                startActivity(intent);
               // adload();


            }
        });



    }
   /* public void adload()
    {

        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }*/


} 

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical"
    tools:context=".Home">

        <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="left"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            app:popupTheme="@color/colorPrimaryDark"
            android:background="@color/colorPrimaryDark"
            app:title="@string/app_name"
            app:titleTextColor="#ffffff"

            />



    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"

            app:cardElevation="10dp"
            android:outlineAmbientShadowColor="#000000"
            android:outlineSpotShadowColor="#000000"
            >
            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/drawable_purple_gradient"
                android:gravity="center_vertical|center_horizontal"
                >
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/java" />



            </androidx.appcompat.widget.LinearLayoutCompat>

        </androidx.cardview.widget.CardView>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:fillViewport="true"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="top"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="2">

                    <androidx.cardview.widget.CardView
                        android:id="@+id/Cview_tuts"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_margin="10dp"
                        android:layout_marginVertical="1dp"
                        android:layout_weight="1"
                        android:clickable="true"
                        android:foreground="?android:attr/selectableItemBackground">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@drawable/circle_cyan"
                            android:gravity="center"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/imageView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp"
                                android:paddingBottom="5dp"
                                app:srcCompat="@mipmap/tut" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:background="@color/colorBG"
                                android:orientation="vertical">

                                <TextView
                                    android:id="@+id/textView1"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_margin="5dp"
                                    android:gravity="center_horizontal"
                                    android:text="Tutorials"
                                    android:textColor="#313131"
                                    android:textSize="14sp"
                                    android:textStyle="bold" />
                            </LinearLayout>

                        </LinearLayout>
                    </androidx.cardview.widget.CardView>

                    <androidx.cardview.widget.CardView
                        android:id="@+id/cView_prog"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_margin="10dp"
                        android:layout_weight="1"
                        android:clickable="true"
                        android:foreground="?android:attr/selectableItemBackground"
                        app:cardBackgroundColor="?attr/colorButtonNormal">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@drawable/circle_purple"
                            android:gravity="center"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/imageView2"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp"
                                android:paddingBottom="5dp"
                                app:srcCompat="@mipmap/prog" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="@color/colorBG"
                                android:orientation="vertical">

                                <TextView
                                    android:id="@+id/textView2"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_margin="5dp"
                                    android:gravity="center_horizontal"
                                    android:text="Programs"
                                    android:textColor="#313131"
                                    android:textSize="14sp"
                                    android:textStyle="bold" />
                            </LinearLayout>

                        </LinearLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="2">

                    <androidx.cardview.widget.CardView
                        android:id="@+id/Cview_IQ"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_margin="10dp"
                        android:layout_marginVertical="1dp"
                        android:layout_weight="1"
                        android:clickable="true"
                        android:foreground="?android:attr/selectableItemBackground"
                        app:cardBackgroundColor="?attr/colorButtonNormal">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@drawable/circle_pink"
                            android:gravity="center"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/imageView3"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp"
                                android:paddingBottom="5dp"
                                app:srcCompat="@mipmap/iques" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="@color/colorBG"
                                android:orientation="vertical">

                                <TextView
                                    android:id="@+id/textView3"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_margin="5dp"
                                    android:gravity="center_horizontal"
                                    android:text="Questions"
                                    android:textColor="#313131"
                                    android:textSize="14sp"
                                    android:textStyle="bold" />
                            </LinearLayout>

                        </LinearLayout>
                    </androidx.cardview.widget.CardView>

                    <androidx.cardview.widget.CardView
                        android:id="@+id/cView_quiz"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_margin="10dp"
                        android:layout_weight="1"
                        android:clickable="true"
                        android:foreground="?android:attr/selectableItemBackground"
                        app:cardBackgroundColor="?attr/colorButtonNormal">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@drawable/circle_green"
                            android:gravity="center"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/imageView4"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp"
                                android:paddingBottom="5dp"
                                app:srcCompat="@mipmap/quiz" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="@color/colorBG"
                                android:orientation="vertical">

                                <TextView
                                    android:id="@+id/textView4"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_margin="5dp"
                                    android:gravity="center_horizontal"
                                    android:text="Quiz"
                                    android:textColor="#313131"
                                    android:textSize="14sp"
                                    android:textStyle="bold" />
                            </LinearLayout>

                        </LinearLayout>
                    </androidx.cardview.widget.CardView>
                </LinearLayout>

            </LinearLayout>

        </ScrollView>

    </androidx.appcompat.widget.LinearLayoutCompat>

</androidx.appcompat.widget.LinearLayoutCompat>

标签: android

解决方案


推荐阅读