动态添加一个view

∥☆過路亽.° 提交于 2019-11-28 01:52:23
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();            }        });    }}

 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!