Android 动态添加View 并设置id

Android 动态添加View 并设置id_第1张图片

主页面布局(main_activity.xml)

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


<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)


<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资源文件

Android 动态添加View 并设置id_第2张图片


<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动画

你可能感兴趣的:(Android,知识点,Android,Demo)