主页面布局(main_activity.xml)

LinearLayout 里面加一个Button,注意这里的LinearLayout要有orientation

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/linear_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="20dp"    tools:context="com.yechaoa.addview.MainActivity">    <Button        android:id="@+id/btn_add_view"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:padding="20dp"        android:text="点击添加view"        android:textSize="20sp"/>LinearLayout>

子View布局(layout_item_view.xml)

<?xml version="1.0" encoding="utf-8"?><TextView    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/text_view"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:padding="20dp"    android:textSize="20sp"/>

点击动态添加View

先找到要添加的view ,然后添加到LinearLayout中

TextView childView1 = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_item_view, mLinearLayout, false);mLinearLayout.addView(childView1);

动态设置id

这里需要先在values文件夹下创建一个ids.xml资源文件

<?xml version="1.0" encoding="utf-8"?><resources>    <item name="text_view_1" type="id">text_view_1item>    <item name="text_view_2" type="id">text_view_2item>    <item name="text_view_3" type="id">text_view_3item>    <item name="text_view_4" type="id">text_view_4item>    <item name="text_view_5" type="id">text_view_5item>resources>

然后通过setId()方法引用这个ids.xml资源文件中的id就行了

textView1.setId(R.id.text_view_1);


MainActivity.java

package com.yechaoa.addview;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.animation.Animation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private LinearLayout mLinearLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        mLinearLayout = findViewById(R.id.linear_layout);        Button mBtnAddView = findViewById(R.id.btn_add_view);        mBtnAddView.setOnClickListener(this);    }    private int childCount = 0;    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.btn_add_view:                childCount++;                TextView childView1 = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_item_view, mLinearLayout, false);                mLinearLayout.addView(childView1);                TextView textView1 = childView1.findViewById(R.id.text_view);                switch (childCount) {                    case 1:                        textView1.setId(R.id.text_view_1);                        textView1.setText(String.valueOf("第 " + childCount + " 个view"));                        initAnimation(textView1, 1);                        break;                    case 2:                        textView1.setId(R.id.text_view_2);                        textView1.setText(String.valueOf("第 " + childCount + " 个view"));                        initAnimation(textView1, 2);                        break;                    case 3:                        textView1.setId(R.id.text_view_3);                        textView1.setText(String.valueOf("第 " + childCount + " 个view"));                        textView1.setTextColor(Color.RED);                        break;                    case 4:                        textView1.setId(R.id.text_view_4);                        textView1.setText(String.valueOf("第 " + childCount + " 个view"));                        break;                    case 5:                        TextView textView = new TextView(MainActivity.this);                        textView.setId(R.id.text_view_5);                        textView.setTextSize(20);                        textView.setGravity(Gravity.CENTER);                        textView.setPadding(20, 20, 20, 20);                        textView.setText(String.valueOf("第 " + childCount + " 个view"));                        textView.setTextColor(Color.BLUE);                        mLinearLayout.addView(textView);                        break;                }                break;        }    }    private void initAnimation(TextView textView, int position) {        switch (position) {            case 1:                TranslateAnimation mLeftAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f,                        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,                        0.0f, Animation.RELATIVE_TO_SELF, 0.0f);                mLeftAnimation1.setDuration(500);                textView.startAnimation(mLeftAnimation1);                textView.animate().alpha(1);                break;            case 2:                TranslateAnimation mLeftAnimation2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,                        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,                        0.0f, Animation.RELATIVE_TO_SELF, 0.0f);                mLeftAnimation2.setDuration(500);                textView.startAnimation(mLeftAnimation2);                textView.animate().alpha(1);                break;        }    }}

关于动画:Android Animation动画

更多相关文章

  1. Android(安卓)之 ActionBar返回按钮
  2. android中无限循环滑动的gallery实例
  3. android实现标题栏、状态栏图标文字颜色及背景动态变化
  4. 查看Android(安卓)ADT Plugin版本的方法
  5. Android(安卓)HorizontalScrollView 水平滑动 在listview上面动
  6. 如果,编译Android时新添加的应用程序没有被编译进image
  7. Android(安卓)Studio的.gitignore以及gitignore无效的解决
  8. Android(安卓)代码修改按钮上的图片
  9. 重点核心☆☆☆☆☆按钮点击事件的四种类型

随机推荐

  1. android checkbox,radiobox style自定义
  2. Phone拨号调起InCallUi流程(Phone 9.0 )(P
  3. 基于Android(安卓)XML解析与保存的实现
  4. Android Drawable Importer的使用
  5. Android中判断字符串中必须包含字母或者
  6. android browser 的几个小feature (四) k
  7. Android SDK R17
  8. Android Selector和Shape的使用方法
  9. android ListView中添加ImageButton按钮
  10. Android Handler机制