FrameLayout实现遮罩层
FrameLayout:该布局container可以用来占有屏幕的某块区域来显示单一的对象,可以包含有多个widgets或者是container,但是所有被包含的widgets或者是container必须被固定到屏幕的左上角,并且一层覆盖一层,不能通过为一个widgets或者是container指定一个位置。Container所包含的widgets或者是container的队列是采用的堆栈的结构,最后加进来的widgets或者是container显示在最上面。所以后一个widgets或者是container将会直接覆盖在前一个widgets或者是container之上,把它们部份或全部挡住(除非后一个widgets或者是container是透明的,必须得到FrameLayout Container的允许)。
以下来自:http://gundumw100.iteye.com/blog/1059685 gundumw100的文章。
利用FrameLayout的特性,可以实现一个简单的遮罩层.
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="show" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Mask" /> </LinearLayout> </FrameLayout>
package com.ql.app; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.FrameLayout; import android.widget.TextView; public class App extends Activity { private boolean isMask = true; private FrameLayout layout = null; private Button btn = null; private TextView textView = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); initViews(); } private void initViews() { layout = (FrameLayout) findViewById(R.id.layout); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new MaskListener()); } // 按钮监听,显示/隐藏遮罩 private class MaskListener implements OnClickListener { public void onClick(View v) { if (isMask) { if(textView==null){ textView = new TextView(App.this); textView.setTextColor(Color.BLUE); textView.setTextSize(20); textView.setText("I am a mask."); textView.setGravity(Gravity.CENTER); textView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); textView.setBackgroundColor(Color.parseColor("#33FFFFFF")); } btn.setText("show"); isMask = false; layout.addView(textView); } else { btn.setText("hide"); isMask = true; layout.removeView(textView); } } } }1 楼 hanjiangit 2012-02-28 遮罩住之后还是可以点击按钮和选中文本框,这是为什么?