我用androidx创建了一个简单的应用程序,但是我对版本有一些疑问。当我开发应用程序时,我使用的是androidx,而我的compileSdkVersion是29,因为这是最新版本。之后,我通过具有29个API(也许是android 10)的电话测试了我的应用。在此之前,一切都很好,但是当我使用具有26个API的手机进行测试时,它无法正常工作。(TextView无法显示其文本,Cardview也不会显示背景色...)我认为问题的原因是API版本。您如何看待?如果您有解决方案,请帮助我。
这是我的 java活动代码
package com.example.lab;
import androidx.appcompat.app.AppCompatactivity;
import android.os.Bundle;
import android.widget.TextView;
public class Mainactivity extends AppCompatactivity {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView)findViewById(R.id.Cloud1);
text2 = (TextView)findViewById(R.id.Cloud2);
text3 = (TextView)findViewById(R.id.Cloud3);
text4 = (TextView)findViewById(R.id.Cloud4);
text5 = (TextView)findViewById(R.id.Cloud5);
text1.setText("C");
text2.setText("L");
text3.setText("O");
text4.setText("U");
text5.setText("D");
}
}
这是我的 XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".Mainactivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/cloud"
tools:ignore="MissingConstraints">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100"
tools:ignore="MissingConstraints">
...
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginRight="10dp"
android:layout_weight="15"
app:cardCornerRadius="100dp"
app:cardBackgroundColor="@color/skyblue">
<TextView
android:id="@+id/Cloud5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
和我的 build.gradle (模块:应用)文件。
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationid "com.example.lab"
minSdkVersion 22
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs',include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.2.0-alpha01'
}
此图片是 API 26
的结果此图片为 API 29