addView android

Android 中LinearLayout中addView妙用并添加子项的监听器

      以前我只知道ListView中可以动态的加载一些项,慢方便的,格式又固定,又可以随意的显示很多项,但是我在添加的项太多了的时候,我发先不行了,特别是添加图片多的时候,就会卡死了,这样我感到很尴尬,好用的东西怎么这么经不起考研呢,百思不得其解,后来有人说LinearLayout有东东可以研究,我去研究了下,发现了一个addView,让我看到了希望。感动之余还是分享一下吧!!
废话少说,还是从布局开始吧:这个布局可以忽略了
<LinearLayout
                        android:orientation="vertical"
                        android:id="@+id/lin_addaddress"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
              >
              <LinearLayout
                      android:orientation="horizontal"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      >
                      <TextView
                              android:text="@string/address"
                              android:layout_width="269dp"
                              android:layout_height="wrap_content"
                              />
                      <Button
                              android:id="@+id/address"
                              android:text="@string/add"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              />
              </LinearLayout>
      </LinearLayout>
其实重点在我们要给LinearLayout加一个ID,我们才可以根据这个Id找到它
其中子项的布局也很简单
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout
        android:id="@+id/linitem1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android"
>
      <!-- 电话号码 -->
      <EditText
            android:id="@+id/editnumber"
            android:layout_width="273dp"
            android:layout_height="wrap_content"
            android:hint="@string/other"
              />
            <Button
                    android:id="@+id/bt_number"
                    android:background="@drawable/jian"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    />
                 

</LinearLayout>
主函数也很简单的,这就是精华吧,呵呵。。。。。
public View getView(String str) {
            Stringinflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater layoutInflater = (LayoutInflater) context
                        .getSystemService(inflater);
            finalLinearLayout linearLayout = (LinearLayout)layoutInflater.inflate(R.layout.additem, null);
            EditTexteditText = (EditText) linearLayout
            .findViewById(R.id.editnumber);
            Buttonbutton = (Button) linearLayout
              .findViewById(R.id.bt_number);
              editText.setHint( str);
              button.setOnClickListener(new View.OnClickListener(){
                  public voidonClick(View v) {
                        EditTexteditText = (EditText)linearLayout.findViewById(R.id.editnumber);
                        String str =editText.getHint().toString();
                        vector.remove(str);
                        linearlayout.removeView(linearLayout);
                       
            }
             
        });
           
            returnlinearLayout;
      }
最后我们定义一个View吧
View view = new View(context);
      view =getView(str);
      linearlayout.addView(view);//添加项
就这么简单,但是是一个很不错的方法,我们可以在上边玩各种花样;
最后还是老一套,给个效果吧!!
Android <wbr>中LinearLayout中addView妙用并添加子项的监听器

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值