Android 动态添加View 并设置id

简介: Android 动态添加View 并设置id

订阅专栏

image.png


主页面布局(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资源文件

image.png



<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="text_view_1" type="id">text_view_1</item>
    <item name="text_view_2" type="id">text_view_2</item>
    <item name="text_view_3" type="id">text_view_3</item>
    <item name="text_view_4" type="id">text_view_4</item>
    <item name="text_view_5" type="id">text_view_5</item>
</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动画

目录
相关文章
|
2天前
|
Android开发
Android Studio(2022.3.1)设置阿里云源-新旧版本
Android Studio(2022.3.1)设置阿里云源-新旧版本
10 1
|
8天前
|
开发工具 Android开发
Android 代码自定义drawble文件实现View圆角背景
Android 代码自定义drawble文件实现View圆角背景
15 0
|
8天前
|
Android开发
Android 自定义View 测量控件宽高、自定义viewgroup测量
Android 自定义View 测量控件宽高、自定义viewgroup测量
10 0
|
8天前
|
XML Java Android开发
Android RecyclerView用代码动态设置item的selector
Android RecyclerView用代码动态设置item的selector
12 0
|
8天前
|
开发工具 Android开发 git
Android自定义View——可以设置最大宽高的FrameLayout
Android自定义View——可以设置最大宽高的FrameLayout
20 0
|
8天前
|
JSON Android开发 数据格式
Android动态添加view设置view大小(宽高)
Android动态添加view设置view大小(宽高)
10 0
|
算法 Android开发
Android自定义控件 | View绘制原理(画多大?)
这一篇将以源码中的几个关键函数为线索分析“测量(measure)”。 如果想直接看结论可以移步到第三篇末尾。 真正的测量工作在onMeasure()中进行。。。
97 0
|
Linux Android开发 开发者
Android窗口管理分析(1):View如何绘制到屏幕上的主观理解
Android窗口管理分析(1):View如何绘制到屏幕上的主观理解
234 0
Android窗口管理分析(1):View如何绘制到屏幕上的主观理解
|
XML Android开发 数据格式