Android 开发报错汇总

3 years ago 高效码农

1、com.android.tools.build:gradle:7.0.3 > com.android.tools:sdk-common:30.0.3 > xerces:xercesImpl:2.12.0 解决方案:添加google()或mavenCentral()或jcenter() ` buildscript { repositories { google() mavenCentral() jcenter() } dependencies { classpath "com.android.tools.build:gradle:7.0.3" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } “`

Android Studio多渠道打包问题汇总(持续更新中)

6 years ago 高效码农

0X01、Error:Removing unused resources requires unused code shrinking to be turned on. 修改build.gradle文件中buildTypes的: minifyEnabled值为true debuggable值为 true buildTypes { debug { signingConfig signingConfigs.release buildConfigField "boolean", "ISDEBUG", "true" debuggable true minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’ } release { signingConfig signingConfigs.release minifyEnabled true zipAlignEnabled true debuggable true shrinkResources true proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’ } } 0X02、WARNING: API ‘variantOutput.getPackageApplication()’ is obsolete and has been replaced with ‘variant.getPackageApplicationProvider()’. WARNING: API ‘variantOutput.getPackageApplication()’ is obsolete and has been replaced with ‘variant.getPackageApplicationProvider()’.It will be removed at the end of 2019.For more information, see https://d.android.com/r/tools/task-configuration-avoidance.To determine what is calling variantOutput.getPackageApplication(), use -Pandroid.debug.obsoleteApi=true on the command line to display a …

Android:RelativeLayout相对布局实现水平方向一个控件居中,一个控件居右

6 years ago 高效码农

在Android开发中经常遇到一个控件居中,一个控件居右的需求;如下原型图 实现代码(目前只用RelativeLayout布局实现了) <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/nickname" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/user_nickname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="@color/white" /> <ImageView android:id="@+id/edit_img" android:layout_width="@dimen/size_10dp" android:layout_height="@dimen/size_10dp" android:layout_marginStart="4dp" android:contentDescription="@null" android:src="@mipmap/ic_wd_bi" /> </LinearLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true"> <TextView android:id="@+id/mine_xuefen" android:layout_width="wrap_content" android:layout_height="22dp" android:layout_marginEnd="-20dp" android:background="@drawable/module_mine_button_credit" android:gravity="center_vertical" android:paddingLeft="25dp" android:paddingRight="30dp" android:text="0学分" android:textColor="@color/textcolor_333333" android:textSize="12sp" /> <ImageView android:layout_width="15dp" android:layout_height="15dp" android:layout_centerVertical="true" android:layout_marginStart="3dp" android:contentDescription="@null" android:src="@mipmap/ic_wd_xing" /> </RelativeLayout> </RelativeLayout> 需要注意的是: 第一个控件LinearLayout需要 android:gravity="center"第二个控件RelativeLayout 需要android:layout_alignParentEnd="true" layout_alignParentEnd和layout_alignParentRight 如果在Android Studio中使用layout_alignParentRight 会警告Consider replacing android:layout_alignParentRight with android:layout_alignParentEnd="true" to better support right-to-left layouts 意思是为了更好的支持阿拉伯语、波斯语等情况下从右往左的阅读方式,从Android 4.2( V 17)开始支持RTL(Right-To-Left)模式。将layout_alignParentRight 替换为layout_alignParentEnd即可。

Android 中this、getContext()、getApplicationContext()、getApplication()、getBaseContext()详解

6 years ago 高效码农

类型 意义 备注 this 当前类是context的子类,一般是activity application等 1、 this:代表当前,在Activity当中就是代表当前的Activity,换句话说就是Activity.this在Activity当中可以缩写为this。2、 Activity.this的context 返回当前activity的上下文,属于activity ,activity 摧毁他就摧毁 getContext() getContext获取的是当前对象所在的Context getApplicationContext() 在当前app的任意位置使用这个函数得到的是同一个Context; 使用getApplicationContext 取得的是当前app所使用的application,这在AndroidManifest中唯一指定 getApplication() andorid 开发中共享全局数据 getBaseContext() 返回由构造函数指定或setBaseContext()设置的上下文 总结: 1.dialog dialog依附于activity存在,所以直接用XXXActivity.this就好,当activity消失的时候dialog也就销毁了 2.activity 上面我们已经说过了,直接使用XXXActivity.this,返回的是当前的activity实例,当前activity销毁时,一起销毁 3.service,broadcastReceiver 两者都可以 总结:和UI操作相关的不建议使用getApplicationContext(),一般都使用和activity相关的context,其余的操作,看具体情况,根据存在的生命周期的长度作出选择 如果是UI控件需要使用Activity作为Context对象,但是默认的Toast实际上使用ApplicationContext也可以。实际上,只要把握住一点,凡是跟UI相关的,都应该使用Activity做为Context来处理;可以看到Activity、Service、Application都是Context的子类