zoukankan      html  css  js  c++  java
  • 动态添加一个视图及其布局属性(要掌握)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.admin.myapplication30.FrameActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/btn_add_frame"
                android:gravity="center"
                android:text="添加视图"
                android:textColor="#000000"
                android:textSize="17sp"/>
    
            <FrameLayout
                android:id="@+id/fl_frame"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:foreground="@mipmap/ic_launcher"
                android:foregroundGravity="top|center_horizontal">
            </FrameLayout>
    
        </LinearLayout>
    
    </android.support.constraint.ConstraintLayout>
    package com.admin.myapplication30;
    
    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;
    
    public class FrameActivity extends AppCompatActivity implements View.OnClickListener {
        private FrameLayout fl_content;
        private int[] mColorArray = {
                Color.BLACK, Color.WHITE, Color.RED, Color.YELLOW, Color.GREEN,
                Color.BLUE, Color.CYAN, Color.MAGENTA, Color.GRAY, Color.DKGRAY
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_frame);
    
            fl_content = (FrameLayout) findViewById(R.id.fl_frame);
            findViewById(R.id.btn_add_frame).setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btn_add_frame) {
                int random = (int) (Math.random()*10 % 10);
                View vv = new View(this);
                vv.setBackgroundColor(mColorArray[random]);
                LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, (random+1)*50);
                vv.setLayoutParams(ll_params);
                vv.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View vvv) {
                        fl_content.removeView(vvv);
                        return true;
                    }
                });
                fl_content.addView(vv);
            }
        }
    }
  • 相关阅读:
    软件工程个人作业01
    学习进度一(2017/12/2)
    课程增加功能(java web)
    剑指offer-把数组排成最小的数
    论文-Edge Boxes
    论文-Selective Search
    剑指offer-机器人的运动范围
    leetcode-539-Minimum Time Difference
    Leetcode-543-Diameter of Binary Tree
    论文-SSD-Single Shot MultiBox Detector
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/7374595.html
Copyright © 2011-2022 走看看