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);
            }
        }
    }
  • 相关阅读:
    NoSQL数据库 continue posting...
    CAP 理论
    Clojure Web 开发 (一)
    HttpClient 4.0.x Tips
    zZ Java中String和Byte[]之间的那些事
    使用nhibernate出现Could not find the dialect in the configuration
    eclipse导入项目出现Project has no default.properties file! Edit the project properties to set one.
    今天开通此博~
    美国白蛾入侵北京 GIS兵法破解危局
    HTML5 存取Json
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/7374595.html
Copyright © 2011-2022 走看看