Toolbar toolbar =findViewById(R.id.toolbar);
if (toolbar!=null){
setSupportActionBar(toolbar);//这里toolbar报红
}
解决方法: 问题出在申明类型时导入了错误的包,
import android.widget.Toolbar;
应该导入
import android.support.v7.widget.Toolbar;
遇到问题2:在actvity_main.xml文件中写入Text View属性时有gravity属性但没有layout_gravity属性提示
解决办法:手动输入layout_gravity 并不会报错
遇到问题3:
运'行报错Error:Execution failed for task ':app:transformClassesWithDexForDebug
这个问题烦了几天,网上资料都说是依赖的包重复了,用gradle命令找到重复的jar包再去掉,尝试着用gradle命令去找到重复的包,然而AS无情的提示我没这个命令,百度,OK,去配置gradle环境变量,配置gradle以为就会顺利吗?呵呵。报错,百度,解决,又报错,could not find from java9 ,真是哔了狗了,GG。
山穷水尽疑无路,柳暗花明又一村。stackoverflow有位老哥遇到了一样的问题,gradle所有依赖直接提到27.1.1,一路绿灯,运行成功,尽管我的手机API是26,并没有肉眼可见的影响。
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
// implementation 'com.android.support:support-v4:26.1.1'
//implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.github.bumptech.glide:glide:4.7.1'
// configurations.all {
// resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
// }
repositories {
mavenCentral()
google()
}
参考https://stackoverflow.com/questions/49043551/multiple-dex-files-define-landroid-support-design-widget-coordinatorlayout1点击打开链接
遇到问题4:
RecyclerView盖住了toolbar
解决方法:
要点1,在coordinatorLayout中toolbar使用AppBarLayout包裹,可以给包裹的子控件设置
app:layout_scrollFlags="scroll|enterAlways"
下拉隐藏toolbar
要点2,在RecylerView中加入
app:layout_behavior="@string/appbar_scrolling_view_behavior"
@string/appbar_scrolling_view_behavior"是系统提供的,用来使滑动控件与AppBarLayout互动。没有这句,RecylerView会直接沾满屏幕,并盖住toolbar
解决。