使用FrameLayout實(shí)現(xiàn)遮罩層
系統(tǒng)
2021 0
利用FrameLayout的特性,可以實(shí)現(xiàn)一個(gè)簡單的遮罩層.
<?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());
}
// 按鈕監(jiān)聽,顯示/隱藏遮罩
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);
}
}
}
}
使用FrameLayout實(shí)現(xiàn)遮罩層
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元