Android ViewManager实例

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重

ViewManagerDemo.Java如下:


packagedev.demo;
importjava.util.LinkedList;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.os.Bundle;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.ViewGroup.LayoutParams;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;


publicclassViewManagerDemoextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
ButtonaddViewButton;
ButtonremoveViewButton;
LinearLayoutmyLayout;
privateLinkedList<TextView>textViews;

booleanisEdited=false;

Contextcontext;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textViews=newLinkedList<TextView>();
findViews();
setListeners();
context=this;
}

privatevoidfindViews(){
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));
}

privatevoidsetListeners(){
if(addViewButton!=null){
this.addViewButton.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){
TextViewmyTextView=newTextView(context);
myTextView.setText("IamnewTextView.");
myTextView.setGravity(Gravity.CENTER);

textViews.addFirst(myTextView);

myLayout.addView(myTextView,
    newViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
//调用addView
if(!isEdited){
isEdited=true;
myLayout.updateViewLayout(textViews.getLast(),
  newLinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
}
});
}

if(removeViewButton!=null){
this.removeViewButton.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){
TextViewtView=textViews.remove();
myLayout.removeView(tView);//移除View
if(isEdited){
isEdited=false;
myLayout.updateViewLayout(textViews.getLast(),
newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
}
});
}
}
}

main.xml如下:


<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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="TextViewNo.1"/>
<TextView
android:id="@+id/textView_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextViewNo.2"/>
<TextView
android:id="@+id/textView_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewNo.3"/>
</LinearLayout>
</LinearLayout>

移动开发交流群:164427941

你可能感兴趣的:(android,实例,ViewManager)