ViewManager的Demo

LinearLayout实现了ViewManager接口。

当LinearLayout调用addView (View view, ViewGroup.LayoutParams params)方法后,LinearLayout中会增加一个子视图,并重新绘制自己。子视图的布局参数是在addView()方法中指定的params。

LinearLayout调用removeView (View view)方法后,LinearLayout会移除view所引用的实例,并重新绘制自己。view必须是LinearLayout中某个子视图对象实例的引用。


LinearLayout调用UpdateViewLayout (View view, ViewGroup.LayoutParams params),会使得view所引用的实例使用params重新绘制自己。iew必须是LinearLayout中某个子视图对象实例的引用。

ViewManagerDemo.java如下:

package com.cnAndroid.api; import java.util.LinkedList; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class ViewManagerDemo extends Activity { /** Called when the activity is first created. */ Button addViewButton; Button removeViewButton; LinearLayout myLayout; private LinkedList<TextView> textViews; boolean isEdited = false; Context context ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textViews = new LinkedList<TextView>(); findViews(); setListeners(); context = this; } private void findViews(){ addViewButton = (Button)this.findViewById(R.id.addView); removeViewButton = (Button)this.findViewById(R.id.removeView); myLayout = (LinearLayout)this.findViewById(R.id.liLayout); textViews.addFirst((TextView)this.findViewById(R.id.textView_1)); textViews.addFirst((TextView)this.findViewById(R.id.textView_2)); textViews.addFirst((TextView)this.findViewById(R.id.textView_3)); } private void setListeners(){ if(addViewButton != null){ this.addViewButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { TextView myTextView = new TextView(context); myTextView.setText("I am new TextView."); myTextView.setGravity(Gravity.CENTER); textViews.addFirst(myTextView); myLayout.addView(myTextView, new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); //调用addView if(!isEdited){ isEdited = true; myLayout.updateViewLayout(textViews.getLast(), new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); } } }); } if(removeViewButton != null){ this.removeViewButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { TextView tView = textViews.remove(); myLayout.removeView(tView); //移除View if(isEdited){ isEdited = false; myLayout.updateViewLayout(textViews.getLast(), new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } } }); } } }

main.xml如下:

<?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" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/addView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加一个TextView" /> <Button android:id="@+id/removeView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除一个TextView" /> </LinearLayout> <LinearLayout android:id="@+id/liLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation = "vertical"> <TextView android:id="@+id/textView_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="TextView No.1"/> <TextView android:id="@+id/textView_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="TextView No.2"/> <TextView android:id="@+id/textView_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView No.3"/> </LinearLayout> </LinearLayout>

效果图如下:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值