动态添加一个view

enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000">
<translate
android:fromXDelta="-100%"
android:toXDelta="0"/>
</set>


out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000">
<translate
android:fromXDelta="0"
android:toXDelta="-100%"/>
</set>


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

globaldialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content">

<Button
android:id="@+id/global_dialog_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="模拟全局对话框"
android:background="@color/colorAccent"
/>
</RelativeLayout>



MainActivty代码

package com.example.globaldialog;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
View decorView;
RelativeLayout v;
Button diss;
Animation enterAnim,outAnim;
private Handler h = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message message) {
v.startAnimation(enterAnim);
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

decorView = getWindow().getDecorView();
enterAnim = AnimationUtils.loadAnimation(this, R.animator.enter);
outAnim = AnimationUtils.loadAnimation(this, R.animator.out);
FrameLayout contentParent = (FrameLayout) decorView.findViewById(android.R.id.content);
v = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.globaldialog,null);
diss = (Button)v.getChildAt(0);
contentParent.addView(v);
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)v.getLayoutParams();
lp.topMargin = 400;
v.setVisibility(View.INVISIBLE);
lp.width=400;
h.sendEmptyMessageDelayed(0,5000);
enterAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
v.setVisibility(View.VISIBLE);
}

@Override
public void onAnimationEnd(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}
});

outAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.INVISIBLE);
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});

diss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
diss.startAnimation(outAnim);
Toast.makeText(MainActivity.this,"hell",Toast.LENGTH_SHORT).show();
}
});
}



}







 
posted @ 2019-08-21 01:10  pushu007  阅读(638)  评论(0编辑  收藏  举报