您的位置:首页 > 移动开发 > Android开发

Android 动态添加View 并设置id

2018-03-22 16:55 633 查看


主页面布局(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
4000
="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_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动画
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: