2016-06-14 3 views
0

Je veux rendre transparent pour ViewPager, mais je ne peux pas trouver un moyen de l'obtenir (il a toujours BLANC couleur, non transparent)est-il possible de définir transparent pour viewpager d'Android

Ma mise en page: main_activity .xml

<?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:background="@drawable/wallpaper"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/view_pager" 
     android:background="#00ffffff" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </android.support.v4.view.ViewPager> 
</LinearLayout> 

la mise en page pour le premier fragment:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="#00ffffff" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="AAAAAAAAAAAAAAAA" 
     android:id="@+id/textView" 
     android:layout_gravity="center_horizontal"/> 
</LinearLayout> 

et la mise en page pour la deuxième fragment:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="#00ffffff" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button" 
     android:layout_gravity="center_horizontal"/> 
</LinearLayout> 

Je définis déjà transparent pour l'arrière-plan de ViewPager et les dispositions pour les fragments. Mais c'est toujours BLANC.

Alors, est-il possible de définir la transparence pour ViewPager? Pouvez-vous l'expliquer pour moi (le cas échéant). Toute suggestion sera appréciée. Je vous remercie.

EDIT: J'ai utilisé le thème par défaut pour l'activité:

enter image description here

Screenshot: screenshot

+0

essayer ceci, hopeit aide: [link] (http://stackoverflow.com/a/9995621/5723796) –

+0

Je définis déjà la couleur de fond de chaque * .xml – Robust

+0

Utilisez-vous déjà le thème "Translucide" dans votre mise en page parente? Quelle est la couleur de votre fond d'écran dans LinearLayout parent? – ErAcube

Répondre

1

Voici le code à ceci: VIEWPAGER

MyFragment.java:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class MyFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.my_fragment, container, false); 
    } 
} 

my_fragment.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="#00ffffff" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="AAAAAAAAAAAAAAAA" 
     android:textColor="#69f0ae" 
     android:id="@+id/textView" 
     android:layout_gravity="center_horizontal"/> 
</LinearLayout> 

Main.java:

FragmentPagerAdapter mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { 
    @Override 
    public Fragment getItem(int position) { 
     return new MyFragment(); 
    } 

    @Override 
    public int getCount() { 
     return 1; 
    } 
}; 
ViewPager mViewPager = (ViewPager) findViewById(R.id.view_pager); 
mViewPager.setAdapter(mPagerAdapter); 

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/wallpaper"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/view_pager" 
     android:background="#00000000" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </android.support.v4.view.ViewPager> 
</android.support.constraint.ConstraintLayout> 
1

Awais Soomro réponse ne fonctionne pas, alors je suis venu avec ceci:

Fragment.java

@Override 
public void onStart() { 
    super.onStart(); 
    getView().setBackgroundColor(Color.TRANSPARENT); 
} 

ou même ...

@Override 
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      view.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      view.setBackgroundColor(Color.TRANSPARENT); 
     } 
    });