Android动态创建布局常用方法

项目中经常需要动态改变某个View的位置,或者自定义View时需要动态赋值等等,因此有一些基础操作需要熟练掌握,下面这个虽然很丑,但是基本上常用的一些动态操作的方法都已经涉及到了,先看下效果,然后对照代码看:

对应代码如下:

public class MainActivity extends AppCompatActivity {

    private RelativeLayout mMainLy;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //第一种添加根布局,在xml中静态设置
        //setContentView(R.layout.activity_main);

        //第二种添加根布局,通过java代码动态添加
        mMainLy = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        setContentView(mMainLy);

        //java代码冬天家自定义View,金币雨动画
        RPEarnCashEntranceView myView = new RPEarnCashEntranceView(getApplicationContext());
        mMainLy.addView(myView);

        //代码创建view,设置属性
        TextView tv = new TextView(getApplicationContext());
        tv.setText("hello Animator");
        tv.setTextColor(Color.argb(255, 255, 0, 0));
        tv.setTextSize(19);
        //设置布局参数
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.BELOW, R.id.hehe2);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lp.addRule(Gravity.CENTER);
        lp.topMargin = 40;
        lp.leftMargin = 200;
        tv.setLayoutParams(lp);

        //文字左边动态添加图片
        Drawable drawableLeft = getResources().getDrawable(R.mipmap.cms_home_icon_coin);
        drawableLeft.setBounds(0,0,drawableLeft.getMinimumWidth(),drawableLeft.getMinimumHeight());
        tv.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableLeft, null,null,null);
        tv.setCompoundDrawablePadding(140);

        //第一种添加根布局
        //mMainLy = (RelativeLayout) findViewById(R.id.main_ly);
        //mMainLy.addView(tv);

        //第二种添加根布局,将上面设置好的参数的view添加到根布局中去
        mMainLy.addView(tv);

        /**
         * 动态添加动画
         */
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv,"translationY",500,-DimenUtils.dp2px(35));
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(tv,"alpha",1f,0.3f);
        objectAnimator1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(objectAnimator,objectAnimator1);
        animatorSet.setDuration(5000);
        animatorSet.start();

        //添加一个自定义画圆圈的View
        CircleView mCircleView = (CircleView) findViewById(R.id.one_key_circle);
        ValueAnimator animator = ObjectAnimator.ofFloat(mCircleView, "alpha", 0f,1f);
        animator.setDuration(5000);
        animator.start();
        mCircleView.startCircle(false);

    }
}

这个项目的完整地址:

https://github.com/buder-cp/base_component_learn/tree/master

猜你喜欢

转载自blog.csdn.net/cpcpcp123/article/details/81747935