Android初学习 - 分别以Java code和XML两种方式给FrameLayout布局的Activity增加一个遮罩效果的实现
2015-07-24 16:40
531 查看
Java code:
对应的布局文件:
-----------------------------------------------
XML FrameLayout 直接实现
layout XML:
color XML:
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>
相关文章推荐
- Android ListView 用法
- 【用户界面:android-Ultra-Pull-To-Refresh】之二:android-Ultra-Pull-To-Refresh框架分析
- Espresso——Android测试工具Espresso简介
- android布局-GridLayout
- Android 无障碍服务一 让应用具有辅助性服务
- 自己写一个快速开发android模板
- Android笔记之四种方式实现计时器
- Android学习笔记【一】Android学习感言(包含学习见解、学习方法)
- 在Android Studio 中加入jar 和.so 文件
- 译文:Android中糟糕的AsyncTask
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- Android实现ListView下拉刷新和上拉加载更多的思路
- 如何快速从android开发转向ios开发--oc语法篇
- Android ViewPager
- Android Studio 快捷键
- 通过Handler与线程嵌套TimerTask实现循环交替任务 分类: Android 2015-07-24 16:14 162人阅读 评论(0) 收藏
- adnroid sdk 更新代理设置
- Android Java中调用js代码,js中调用Java代码
- Android属性动画完全解析(上),初识属性动画的基本用法
- Android事件模型之interceptTouchEvnet ,onTouchEvent关系正解