安卓ui设计之动态添加view

在ui设计当中,有时候需要动态添加动态view。这时候安卓提供了一些动态添加view的接口。注意addView是相对ViewGropu来说的,单独的View没有addView方法。

addView(View child)   // child:添加的View
addView(View child, int index)   // index:添加的View的索引
addView(View child, int width, int height)   // width,height:指定添加View的宽高值
addView(View view, ViewGroup.LayoutParams params)   // params:添加的View指定的布局参数
addView(View child, int index, LayoutParams params) //上面多个参数同时添加

xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:orientation="vertical"
        android:id="@+id/m_container">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="一开始就有的view"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="@color/colorAccent">

        <Button
            android:id="@+id/add_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="添加view"
            android:layout_gravity="center_horizontal"/>

    </LinearLayout>

</LinearLayout>

代码逻辑:
每当点击按钮的时候就自动在id为m_container的LinearLayout中添加一个textview,并打印添加的个数。注意addView是相对ViewGropu来说的,单独的View没有addView方法。这里我们的LinearLayout本身就是一个ViewGropu类型。

package com.gentle.addview;


import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private int addCount = 0;
    private LinearLayout mContent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContent = findViewById(R.id.m_container);

        final Button addViewBtn = findViewById(R.id.add_view);
        addViewBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("lzt","setaddCount"+addCount);
                addCount++;

                if(mContent != null){
                    addView(addCount);
                }else
                    Log.d("lzt","mContent is null");

            }
        });
    }


    private void addView(int count){
        TextView child = new TextView(this);
        child.setTextSize(16);
        child.setTextColor(getResources().getColor(R.color.colorAccent));
        String text= String.valueOf(count);
        text = "添加了一个view" + text;
        child.setText(text);
        mContent.addView(child);
    }


}

运行效果:
由于标题栏没有去掉,所以挡住了前面一两次的textview.
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值