通过实例说明:
1:父view启动子view
public class MainActivity extends Activity {
private Button StartViewBut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartViewBut = (Button) findViewById(R.id.button);
StartViewBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new SubView(MainActivity.this).show();//启动一个子view
}
});
}
}
父view对应的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="******start sub view******"
android:gravity="center"/>
</LinearLayout>
2:子view处理点击事件
public class SubView extends LinearLayout implements OnClickListener {
private Context mContext;
private Button OkBtn, CancelBtn;
private TextView text;
public SubView(Context context) {
super(context);
mContext = context;
/*((Activity) mContext).addContentView(this, new LayoutParams(-1,-1));
LayoutInflater.from(mContext).inflate(R.layout.layout_sub, this);*/
View subView = LayoutInflater.from(mContext).inflate(R.layout.layout_sub, this);
((Activity) mContext).addContentView(subView, new LayoutParams(-1,-1));
text = (TextView) findViewById(R.id.display);
OkBtn = (Button) this.findViewById(R.id.button_ok);
CancelBtn = (Button) this.findViewById(R.id.button_cancel);
this.setOnClickListener(this);
OkBtn.setOnClickListener(this);
CancelBtn.setOnClickListener(this);
this.setVisibility(View.GONE);
}
public void show(){
this.setVisibility(View.VISIBLE);
}
public void cancel(){
this.setVisibility(View.GONE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_ok:
text.setText("success!");
break;
case R.id.button_cancel:
text.setText("fail!");
break;
default:
this.setVisibility(View.GONE);
break;
}
}
}
说明:
a:实现子view的页面布局加载在父view页面布局之上的两种方法
((Activity) mContext).addContentView(this, new LayoutParams(-1,-1));
LayoutInflater.from(mContext).inflate(R.layout.layout_sub, this);
View subView = LayoutInflater.from(mContext).inflate(R.layout.layout_sub, this);
((Activity) mContext).addContentView(subView, new LayoutParams(-1,-1));
b:实现点击子View的时候父view有无响应
this.setOnClickListener(this);//无响应
this.setOnClickListener(this);//有响应
c:控制子view显示时机
public void show(){
this.setVisibility(View.VISIBLE);
}
public void cancel(){
this.setVisibility(View.GONE);
}
d:点击子view,是否可以回到父控件
default:
this.setVisibility(View.GONE);
break;//回到父view
default:
break;//不回到父view
子view对应的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#B4000000"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:textColor="#ff0000"
android:gravity="center"
android:text="*******success or fail ?******" />
<TextView
android:id="@+id/display"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:textColor="#ff0000"
android:gravity="center"
android:text="*******display data******" />
<Button
android:id="@+id/button_ok"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:text="OK" />
<Button
android:id="@+id/button_cancel"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:text="CANCEL" />
</LinearLayout>