Android初学习 - 分别以Java code和XML两种方式给FrameLayout布局的Activity增加一个遮罩效果的实现

Java code:

package com.app.test02;  
   
import android.app.Activity;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.view.Gravity;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.view.Window;  
import android.widget.Button;  
import android.widget.FrameLayout;  
import android.widget.TextView;  
   
   
public class ShadeActivity extends Activity {  
    // 设置是否展开  
    private boolean isFolded = true;  
    // 设置控件  
    private FrameLayout layout = null;  
    private Button unfoldButton = null;  
    private TextView textView = null;  
   
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_shade);  
   
        initView();  
    }  
   
    @Override 
    protected void onResume() {  
        super.onResume();  
        isFolded = true;  
    }  
   
    // 初始化  
    private void initView() {  
        layout = (FrameLayout) findViewById(R.id.layout);  
        unfoldButton = (Button) findViewById(R.id.unfoldButton);  
        unfoldButton.setOnClickListener(new UnfoldClickListener());  
    }  
   
    // 按钮监听,展开一个透明的显示文本的遮挡层  
    private class UnfoldClickListener implements OnClickListener {  
        public void onClick(View v) {  
            if (isFolded) {  
                textView = new TextView(ShadeActivity.this);  
                textView.setGravity(Gravity.CENTER);  
                textView.setLayoutParams(new ViewGroup.LayoutParams(  
                        ViewGroup.LayoutParams.MATCH_PARENT,  
                        ViewGroup.LayoutParams.MATCH_PARENT));  
                textView.setBackgroundColor(Color.parseColor("#55000000"));  
   
                unfoldButton.setText("取消遮罩");  
   
                isFolded = false;  
   
                layout.addView(textView);  
            } else {  
                unfoldButton.setText("显示遮罩");  
                isFolded = true;  
                layout.removeView(textView);  
            }  
        }  
    }  
}

对应的布局文件:

<?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"  
    android:background="#fff">  
   
    <Gallery 
        android:id="@+id/showGallery" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:spacing="0dip" />  
   
    <RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom" 
        android:background="#86222222" 
        android:orientation="horizontal" >  
   
        <TextView 
            android:id="@+id/titleTextView" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="遮罩效果" 
            android:textColor="#ff0000" />  
   
        <Button 
            android:id="@+id/unfoldButton" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_alignParentRight="true" 
            android:text="显示遮罩" />  
    </RelativeLayout>  
   
</FrameLayout>


-----------------------------------------------

XML FrameLayout 直接实现

layout XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_fm_layout" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--  具体布局 ... -->

    </LinearLayout>

    <!-- 此处为遮罩层 -->
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/mask_bg_color" />

</FrameLayout>


color XML:

<resources> 
    <color name="mask_bg_color">#55000000</color>
</resources> 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值