Android获取动态布局的的控件

一、先写一个动态布局

1、这个效果就是让控件点一变大

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        setContentView(initView());
    }

    private View initView() {
        RelativeLayout relativeLayout = new RelativeLayout(this);
        final Button bigButton = new Button(this);
//        bigButton.setBackground(getDrawable(android.R.color.holo_blue_bright));
        bigButton.setText("启动");
        relativeLayout.addView(bigButton);
        bigButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ViewGroup.LayoutParams bb = bigButton.getLayoutParams();
                bb.width += 200;
                bb.height += 200;
                bigButton.requestLayout();

            }
        });
        return relativeLayout;
    }
}

2、在这里面貌似没有找到类似XMl布局获取ID的方法,我们在代码里添加布局,是没有生成映射的id的,所以我解决的就是,获取动态布局的全局变量,这个全局变量就是一个控件,

public class RelativePopsView extends RelativeLayout {
    public ImageView ivCloce; // 图标关闭按钮
    private Context context; 
    public ScreenPopsView screenPopsView; // 显示的图片

    public RelativePopsView(Context context) {
        this(context, null);
    }

    public RelativePopsView(Context context, AttributeSet attrs) {
        this(context, null, 0);
    }

    public RelativePopsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;

        init();

    }

    private void init() {
        // TODO Auto-generated method stub
        screenPopsView = new ScreenPopsView(context, 20); 
        screenPopsView.setId(Utils.generateViewId());
        screenPopsView.setLayoutParams(
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        addView(screenPopsView);
        ivCloce = new ImageView(context);
relativePopsView = new RelativePopsView(ctx);
 screenPopsView = relativePopsView.screenPopsView;//然后可以直接操作这个我们需要的控件了

        //设置最外层FrameLayout布局
        final FrameLayout fl = new FrameLayout(context);
        FrameLayout.LayoutParams flParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        fl.setLayoutParams(flParams);
        fl.setBackgroundColor(Color.BLACK);


        //设置 RelativeLayout布局
        RelativeLayout rl = new RelativeLayout(context);

        FrameLayout.LayoutParams rlp = new FrameLayout.LayoutParams(500, 500);
        rlp.gravity = Gravity.CENTER;       //这两行设置剧中显示 , 使用FrameLayout  属性中 android:layout_gravity  设置居中

        rl.setBackgroundColor(Color.BLUE);
        rl.setLayoutParams(rlp);
        //设置图片
        ImageView imageView = new ImageView(context);    //设置添加的图片
        RelativeLayout.LayoutParams ivParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rl.addView(imageView, ivParams);
        imageView.setImageResource(R.mipmap.girl);

        fl.addView(rl, rlp);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值