xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <Button 7 android:id="@+id/btn_add_frame" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:gravity="center" 11 android:text="给下方的框架布局添加视图" 12 android:textColor="#000000" 13 android:textSize="17sp" /> 14 15 <FrameLayout 16 android:id="@+id/fl_content" 17 android:layout_width="match_parent" 18 android:layout_height="0dp" 19 android:layout_weight="1" 20 android:foreground="@mipmap/ic_launcher" 21 android:foregroundGravity="top|center_horizontal" > 22 </FrameLayout> 23 24 </LinearLayout>
java
1 package com.example.alimjan.hello_world; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.graphics.Color; 6 import android.os.Bundle; 7 import android.support.v7.app.ActionBar; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.widget.FrameLayout; 11 import android.widget.LinearLayout; 12 13 /** 14 * Created by alimjan on 7/2/2017. 15 */ 16 17 18 public class class_3_1_2 extends AppCompatActivity implements View.OnClickListener { 19 20 private FrameLayout fl_content; 21 private int[] mColorArray = { 22 Color.BLACK, Color.WHITE, Color.RED, Color.YELLOW, Color.GREEN, 23 Color.BLUE, Color.CYAN, Color.MAGENTA, Color.GRAY, Color.DKGRAY 24 }; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.code_3_1_2); 30 31 fl_content = (FrameLayout) findViewById(R.id.fl_content); 32 findViewById(R.id.btn_add_frame).setOnClickListener(this); 33 } 34 35 @Override 36 public void onClick(View v) { 37 if (v.getId() == R.id.btn_add_frame) { 38 int random = (int) (Math.random()*10 % 10); 39 View vv = new View(this); 40 vv.setBackgroundColor(mColorArray[random]); 41 LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams( 42 ActionBar.LayoutParams.MATCH_PARENT, (random+1)*50); 43 vv.setLayoutParams(ll_params); 44 vv.setOnLongClickListener(new View.OnLongClickListener() { 45 @Override 46 public boolean onLongClick(View vvv) { 47 fl_content.removeView(vvv); 48 return true; 49 } 50 }); 51 fl_content.addView(vv); 52 } 53 } 54 55 public static void startHome(Context mContext) { 56 Intent intent = new Intent(mContext, class_3_1_2.class); 57 mContext.startActivity(intent); 58 } 59 60 }
时间: 2024-10-09 07:38:17