利用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);
}
}
}
}
- Test_Mask.rar (41.3 KB)
- 下载次数: 336
-
- 大小: 20.2 KB
-
- 大小: 34 KB
- 查看图片附件