1
votes

Android LinearLayout, définir l'arrière-plan sur Transparent

Je gonfle un bottom_sheet_layout . Il contient un LinearLayout comme élément racine. Il a un enfant CardView.

J'ai essayé ce qui suit:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />

Ceci est la BottomSheet.Class qui est liée au bottom_sheet_layout code >

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    android:id="@+id/root_element"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_margin="16dp">

<!-- A CardView that contains a TextView -->
<androidx.cardview.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/poiName"
        android:fontFamily="@font/comfortaa_bold"
        android:text="@string/poiName"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="20dp"/>

    <!--Location-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="10dp"
            android:fontFamily="@font/work_sans_medium"
            android:text="@string/ui_location"
            android:textFontWeight="800" />

        <TextView
            android:id="@+id/location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="10dp"
            android:fontFamily="@font/work_sans"
            android:text="@string/ui_location_addr" />

    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/share"
        android:scaleType="center"
        card_view:backgroundTint="@color/colorPrimary" />

    </LinearLayout>

</androidx.cardview.widget.CardView>
</LinearLayout>

Ceci est la bottom_sheet_layout

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
LinearLayout l = container.findViewById(R.id.root_element);
l.setBackgroundColor(Color.TRANSPARENT);
return v;
}

Voici la carte Fragment . Voici où je gonfle la bottom_sheet_layout

setting `android:background` property to 
    1. #00FFFFFF
    2. #00000000
    3. @android:color/transparent

setting the background color of LinearLayout programatically

    `LinearLayout l = container.findViewById(R.id.root_element);
     l.setBackgroundColor(Color.TRANSPARENT);`

Suite à plusieurs guides, je n'ai toujours pas pu obtenir la méthode # 00FFFFFF pour travailler. Définir la couleur par programme sur transparent, plante l'application.

Il semble que dans les questions précédentes sur StackOverflow, définir l'arrière-plan sur le code hexadécimal avec la valeur alpha utilisé pour fonctionner. Maintenant, je n'obtiens plus qu'un fond gris pâle.

Captures d'écran de l'application

  1. Mise en page de l'image
  2. Capture d'écran de l'application


12 commentaires

Partagez-moi votre fichier de mise en page xml bottom_sheet_layout.xml


veuillez ajouter le fichier xml de la mise en page


Edit: la mise en page XML a été ajoutée à la question


pourquoi ne supprimez-vous pas simplement le LL s'il ne s'agit que d'un conteneur transparent qui ne fait rien


Mais, alors le CardView s'étend automatiquement sur toute la largeur, et je ne peux même pas voir les bordures de la carte. Cela devient simplement une simple feuille de fond modale. Le LinearLayout permet au moins de conserver la forme de la carte.


Cette disposition est-elle chargée dans un ` 'à partir d'une autre disposition? Si oui, veuillez partager cette autre mise en page XML.


J'ai 3 feuilles XML, 1. MainActivity (ne fait rien) 2. MapsActivity (Fragment qui charge google maps) 3. BottomSheetActivity (Le code XML est affiché dans la question) Je gonfle uniquement la feuille de fond dans l'activité des cartes.


Alors, que voulez-vous voir en arrière-plan de bottom_sheet_layout ? Les cartes Google? Si oui, partagez votre mise en page XML MapsActivity.


Sûr! S'il vous plaît voir la mise à jour


Et où se trouve votre objet MapView ? Par programme?


Pourriez-vous s'il vous plaît préciser où pourrais-je trouver l'objet MapView ?


S'il vous plaît, mettez @Ferran devant votre message, sinon je ne serai pas informé. Voyez ma réponse.


4 Réponses :


0
votes

Essayez background null.

<LinearLayout
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:background="@null">

        ......

      </LinearLayout>


3 commentaires

Merci pour la suggestion, cependant, cela n'a pas fonctionné.


En fait, cela a fonctionné, mais je ne pouvais pas comprendre pourquoi jusqu'à ce que je réalise que mon BottomSheetActivity était celui avec un arrière-plan solide, où je gonflais mon bottom_sheet_layout . J'ai suivi la solution de @Ferran en combinaison avec votre solution.


@ vDevel_00: Super, allez-y +1



0
votes

Êtes-vous sûr que les éléments de mise en page enfants n'ont pas d'arrière-plan?


1 commentaires

Oui. Aucune des mises en page enfant n'a de propriété d'arrière-plan



1
votes

Je pense que je peux vous aider avec une sorte de solutions, que vous pouvez également essayer et voir si cela fonctionne ou non.

  1. Utilisez la propriété alpha dans votre fichier .java

Définissez la valeur en fonction de votre facilité

<LinearLayout
  android:id="@+id/l1"
  android:alpha="0.5"
  android:layout_width="190dp"
  android:layout_height="match_parent">
  1. Vous pouvez également utiliser alpha dans votre fichier XML:
LinearLayout l = container.findViewById(R.id.root_element);
l.setAlpha(0.4);

0.0 est pour totalement transparent et 1.0 pour totalement opaque.

Faites-moi savoir si l'une des solutions fonctionne pour vous. Cependant, faites plus de recherches ici, vous obtiendrez certainement une solution. Bon codage.


2 commentaires

Cela n'a pas fonctionné, car je suis en train de définir un alpha sur l'élément racine LInearLayout. L'utilisation de votre solution rend les éléments enfants plus légers, presque comme les éléments enfants héritent de la propriété alpha du parent.


Veuillez regarder cette image ici Ceci est le résultat lorsque je règle la valeur alpha par programme ou via xml. Et ceci ici C'est le résultat sans jeu alpha.



1
votes

Si vous n'utilisez pas MapView , je suppose que vous gonflez Google Map + bottom_sheet_layout dans le même fragment. Si oui, je pense que votre problème n'est pas la transparence LinearLayout, mais vous insérez bottom_sheet_layout au bas du conteneur de fragments, pas sur la carte.

Vous pouvez utiliser cette mise en page pour votre fragment de carte. (androidx, mais très similaire si vous utilisez Android)

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

    <!-- use this fragment for your google map -->
    <fragment
        android:id="@+id/google_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


    <!-- use this fragment for your bottom_sheet class -->    
    <fragment
        android:id="@+id/bottom_sheet"
        android:layout_margin="16dp"            // with this you do not need the LinearLayout root_element 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Ensuite, vous pouvez jouer avec votre CardView (ou LinearLayout code>) visibilité pour afficher la carte ou non.

D'autres mises en page sont possibles comme utiliser directement la vue carte au lieu d'un Fragment . p >


2 commentaires

Oui, je me suis rendu compte que l'activité de la feuille du bas était le coupable de cette solide expérience, après de longues heures de frustration. Cependant, j'ai un peu modifié votre réponse. J'ai utilisé la balise pour obtenir mon bottom_sheet_layout , puis j'y ai ajouté un BottomSheetBehaviour .


@ vDevel_00 Génial. Heureux de vous aider.