android viewGroup中addView使用方式及其粗解

原文:http://write.blog.csdn.net/postedit/50562162

addView是继承viewGroup的方法,

void android.view.ViewGroup.addView(View child);

void android.view.ViewGroup.addView(View child, LayoutParams params);

void android.view.ViewGroup.addView(View child,int index, LayoutParams params);


其中需要注意的是 index ,在linearlayout中使用addView的时候,如果linearlayout方向是vertical 垂直, index代表添加的child的view在linearlayout的行数,index是0,表示添加的child在linearlayout顶部,-1为底部。位置可随意调整。


如下demo:


常见使用方式:

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

LinearLayout relativeLayout = (LinearLayout)findViewById(R.id.test2);
     View container = LayoutInflater.from(this).inflate(R.layout.add_content, null);
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
     relativeLayout.addView(container, 0, params);

// relativeLayout.addView(container, -1, params);

//relativeLayout.addView(container, 1, params);

//relativeLayout.addView(container, 2, params);

}


activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/test2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:background="@android:color/background_light"
            android:layout_alignParentTop="true"
            android:text="2222222222" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_below="@id/tv1"
            android:background="@android:color/black"
            android:text="333333333333333" />
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


add_content.xml:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:text="11111TextView111111" />

</RelativeLayout>

原文:http://write.blog.csdn.net/postedit/50562162



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值