求解:android中如何实现动态插入控件

求解:android中如何实现动态插入控件,第1张

直接给你上代码吧,写了我半个小时,经过了我的测试了的~

运行下就能看到结果了~关键的remove的时候有给你写注释~

布局的layout文件内容:

----------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" android:id="@+id/linearlayout">

<LinearLayout android:id="@+id/LinearLayout01"

android:layout_width="wrap_content" android:layout_height="wrap_content">

<Button android:layout_height="wrap_content" android:id="@+id/add"

android:text="Add" android:layout_width="100px"></Button>

<Button android:layout_height="wrap_content"

android:layout_width="100px" android:text="Remove" android:id="@+id/remove"></Button>

</LinearLayout>

<TextView android:id="@+id/TextView01" android:text="This is textView."

android:layout_width="fill_parent" android:gravity="center"

android:layout_height="50px"></TextView>

</LinearLayout>

----------------------------------------------------------------------------------

对应Activity的内容:

----------------------------------------------------------------------------------

package com.foxconn.dialog

import android.app.Activity

import android.os.Bundle

import android.view.View

import android.view.View.OnClickListener

import android.view.ViewGroup.LayoutParams

import android.widget.Button

import android.widget.LinearLayout

public class DialogTest extends Activity implements OnClickListener {

private Button add_btn, remove_btn

private LinearLayout linearLayout

private int index = 0

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.main)

findViews()

register()

}

private void register() {

add_btn.setOnClickListener(this)

remove_btn.setOnClickListener(this)

}

private void findViews() {

add_btn = (Button) findViewById(R.id.add)

remove_btn = (Button) findViewById(R.id.remove)

linearLayout = (LinearLayout) findViewById(R.id.linearlayout)

}

protected View createView() {

Button btn = new Button(this)

btn.setId(index++)

btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))

btn.setText("aaaaaa" + index)

return btn

}

private void removeView() {

//获取linearlayout子view的个数

int count = linearLayout.getChildCount()

//研究整个LAYOUT布局,第0位的是含add和remove两个button的layout

//第count-1个是那个文字被置中的textview

//因此,在remove的时候,只能 *** 作的是0<location<count-1这个范围的

//在执行每次remove时,我们从count-2的位置即textview上面的那个控件开始删除~

if (count - 2 >0) {

//count-2>0用来判断当前linearlayout子view数多于2个,即还有我们点add增加的button

linearLayout.removeViewAt(count - 2)

}

}

public void onClick(View v) {

switch (v.getId()) {

case R.id.add:

linearLayout.addView(createView(), 1)

break

case R.id.remove:

removeView()

break

default:

break

}

}

}

----------------------------------------------------------------------------------

可以的,android中使用布局是为了加快开发,最终控件还是通过解析XML后,通过代码添加的。

具体方法:

例如你的布局是一个Linearlayout linear上面有一本Button btn1

要删除这个btn1要做的就是 linear.removeView( btn1 )

动态添加也是一样的

linear.addView( btn2 )

要注意的是,Button btn2 = new Button( context )这时候控件是没有大小的,必须设置控件大小以后添加了才能看到。设置控件大小的方法是view.setLayoutParams()

希望能够帮到你。

在代码中可以动态的添加、移除控件;可以先获得一个layout的根标签layoutview,然后使用layoutview.addview(XXX),layoutview.removeview(XXX)之类的方法,动态添加、移除view,代码中也是可以控制布局的,找点资料看看就明白了,几句话也说不太明白。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11610489.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存