2017-06-17 62 views
0

我想创建一种透明的教程,第一次只出现 这是我创建的片段,如何将它添加到顶部现有布局在现有布局的顶部添加一个透明的片段(android0

这里的片段

import android.app.Fragment; 
import android.content.SharedPreferences; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 

import in.webblue.nuclity.Activity.Logs.SaveLog; 
import in.webblue.nuclity.R; 

import static android.content.Context.MODE_PRIVATE; 

/** 
* Created by Akshay on 15-06-2017. 
*/ 

public class TutorialFragment extends Fragment { 
    private String Class_Name = "TutorialFragment"; 
    private boolean ranBefore; 
    View topLevelLayout1; 
    View topLevelLayout2; 
    View myView; 
    String methodName = "onCreateView"; 

    public static TutorialFragment newInstance() { 
     TutorialFragment f = new TutorialFragment(); 
     return f; 
    } 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     myView = inflater.inflate(R.layout.tutorial_layout, container, 
    false); 
     topLevelLayout1=myView.findViewById(R.id.tutorial1); 
     topLevelLayout2=myView.findViewById(R.id.tutorial2); 
     if (!isFirstTime()) { 
      topLevelLayout1.setVisibility(View.INVISIBLE); 
      topLevelLayout2.setVisibility(View.INVISIBLE); 

     } 
    return myView; 
} 

    private boolean isFirstTime() 
    { 
     try { 
      SharedPreferences preferences = 
     this.getActivity().getSharedPreferences("RanBefore", MODE_PRIVATE); 
      boolean ranBefore = preferences.getBoolean("RanBefore", false); 
      if (!ranBefore) { 

       SharedPreferences.Editor editor = preferences.edit(); 
       editor.putBoolean("RanBefore", true); 
       editor.commit(); 
       topLevelLayout1.setVisibility(View.VISIBLE); 
       topLevelLayout2.setVisibility(View.INVISIBLE); 
       topLevelLayout1.setOnTouchListener(new 
       View.OnTouchListener() { 

        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         topLevelLayout1.setVisibility(View.INVISIBLE); 
         topLevelLayout2.setVisibility(View.VISIBLE); 
         return false; 
        } 

       }); 
       topLevelLayout2.setOnTouchListener(new 
      View.OnTouchListener() { 

        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         topLevelLayout2.setVisibility(View.INVISIBLE); 
         return false; 
        } 

       }); 


      } 
     } 
     catch (Exception e){ 
      Log.e(getClass().getName(),"Method Name :"+methodName+ " "+ e.getStackTrace().toString()); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

     SaveLog.saveLog(getContext(),Class_Name,methodName,e.toString()); 
      } 
     } 
     return ranBefore; 

    } 

} 

我需要添加这在现有布局

回答

0

顶部的代码,我想是的FrameLayout去这里的路。

FrameLayout的秘密就是它如何布局它的子项。尽管通常设计为包含一个项目,但它会愉快地将其他元素叠加在一起。这样的FrameLayout本质上是一种方式来操纵的意见Z-顺序屏幕

下面就关于什么的FrameLayout可以做螺纹:
what does FrameLayout do?

所以,你的布局会是这个样子:

<FrameLayout> 
    <Fragment/> 
    <LinearLayout> 
    // here is your normal layout 
    </LinearLayout> 
</> 
+0

不解决问题 –

0

你可以用下面的方法做。 这是您最需要添加片段的活动。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


    <LinearLayout 
     android:id="@+id/example_fragment_parent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     //YOUR LAYOUT 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/example_fragment_parent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:onClick="EndFragment"> 

     /*THE FRAGMENT YOU WANT TO SHOW. THE LOGIC TO SHOW THIS FRAGMENT 
      ONLY ONCE WILL HAVE TO BE IN THE ACTIVITY ON TOP OF WHICH YOU ARE 
      SHOWING THIS FRAGMENT*/ 

     <fragment 
      android:id="@+id/example_fragment" 
      android:name="com.example.ExampleFragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

    </LinearLayout> 

</RelativeLayout> 

现在你应该做的就是只显示一次该片段。 OnClick方法来隐藏这个片段如下:

public void EndFragment(View view) { 
    example_fragment_parent.setVisibility(View.GONE); 
} 

你需要找到这个片段在OnCreate()你的活动象下面这样:

LinearLayout example_fragment_parent = 
(LinearLayout)findViewById(R.id.example_fragment_parent); 
相关问题