Android ViewGroup中addView方法使用

Android 中ViewGroup 中的添加子控件的方法addVIew方法

//child:要添加的view
public void addView(View child) ;

//child:要添加的View  index:view在父布局中位于第几个
public void addView(View child, int index);

//child:要添加的View  width/height:在父布局中的宽高
public void addView(View child, int width, int height);

//child:要添加的View  params:在父布局中的参数
public void addView(View child, LayoutParams params);
//child:要添加的View  ;index:view在父布局中位于第几个; params:在父布局中的参数
public void addView(View child, int index, LayoutParams params);

index 参数在linearlayout中使用addView的时候,如果Linearlayout方向是vertical 垂直, index代表添Linearlayout中第几个子view例如

布局文件:

<?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="com.phone.test_addview.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/content"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1111111111"
            android:gravity="center"
            android:layout_margin="20dp"
            android:textSize="18sp"
            android:textColor="#f1f"
        />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2222222222"
            android:gravity="center"
            android:layout_margin="20dp"
            android:textSize="18sp"
            android:textColor="#f1f"
            />
    </LinearLayout>
</LinearLayout>

Activity文件:

package com.phone.test_addview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout content= (LinearLayout) findViewById(R.id.content);
        Button button =new Button(this);
        button.setText("确定");
        LinearLayout.LayoutParams param=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        param.gravity= Gravity.CENTER;
        content.addView(button,1,param);
    }
}

这里写图片描述

以上代码就是在两个TextView中间添加一个Button,index参数-1代表就是在最后添加,0代表第一个子元素,1是第二个以此类推

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值