我给了一个id属性:
android:id =“ @ + id / animation_layout”
在activity_main.xml的默认布局(编号为1)中
但是当我尝试在MainActivity.java中通过其ID调用它时: ConstraintLayout约束布局= findViewById(R.id.animation_layout);
我想念什么?
还附上了代码:
<android.support.constraint.ConstraintLayout 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:id="@+id/animation_layout"
android:background="@drawable/gradients_list"
tools:context=".MainActivity">
和Java代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Gradient animation
ConstraintLayout constraintLayout = findViewById(R.id.animation_layout);
AnimationDrawable animationDrawable = (AnimationDrawable) constraintLayout.getBackground();
animationDrawable.setEnterFadeDuration(2000);
animationDrawable.setEnterFadeDuration(4000);
animationDrawable.start();
感谢您的帮助:)