蒙版的实现(使用Fragment)

我们这里实现一个用Fragment做的蒙版,当点击的时候我们让其消失,这里做的蒙版我们可以给我们想要view版设置。
步骤:
1.首先我们需要一个主界面,主界面的根布局必须是FrameLayout(我试过用LinearLayout,RelativeLayout,但是都没用,我也不知道为什么,求大神告知! )。
    
    
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mengbang.MainActivity">
 
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
 
</FrameLayout>
2.我们需要一个Fragment来做为蒙版,那么我们创建一个Fragment,并给这个fragment设置点击事件,我这里用了接口让这个fragment消失。
   
   
package com.example.mengbang;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class MyJobDetailsAssertFragment extends Fragment {
 
public interface OnremoveFragment {
void remove();
}
 
public void setOnremoveFragment(OnremoveFragment onremoveFragment) {
mOnremoveFragment = onremoveFragment;
}
 
private OnremoveFragment mOnremoveFragment;
 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_show, null);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnremoveFragment.remove();
}
});
return view;
}
}
4.还有就是布局了,我们的布局我们需要的背景颜色可以设置为#55000000,当然你们可以选择一些其他的颜色试试,这里我在布局上加了一个图片,是为了告诉大家我们可以在蒙版上做我们想做的事。
   
   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#55000000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
>
 
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:paddingTop="35dp"
android:layout_gravity="left"
android:src="@drawable/ic_launcher" />
 
</LinearLayout>
</LinearLayout>
5.接下来就是MainActivity了, MainActivity很简单,我们只是将我的fragment给添加了,然后在fragment的回调方法中给remove掉了这个fragment,添加的时候我是用了view树监听,当监听到我们的主界面的布局完全显示出来了后我们再进行添加fragment,不然我们的主界面的布局没有显示出来我们的fragment的布局是无法显示的。
   
   
package com.example.mengbang;
 
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewTreeObserver;
 
public class MainActivity extends FragmentActivity {
private View rootView;
private MyJobDetailsAssertFragment detailsAssertFragment = new MyJobDetailsAssertFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootView = findViewById(R.id.root);
 
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
getSupportFragmentManager().beginTransaction()
.add(R.id.root, detailsAssertFragment)
.commit();
}
});
 
detailsAssertFragment.setOnremoveFragment(new MyJobDetailsAssertFragment.OnremoveFragment() {
@Override
public void remove() {
getSupportFragmentManager().beginTransaction().remove(detailsAssertFragment).commit();
}
});
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值