2017-10-11 6 views
0

J'ai ajouté une vue au bas de ma mise en page pour ajouter un séparateur de ligne à la fin de cet élément. Le problème est qu'après avoir placé cette vue dans la disposition remplit toute la largeur de l'écran mais quand je prends la vue elle montre la taille correcte (environ 400dp dans mes tests). Pourquoi la vue ajoutée entraîne-t-elle la mise en page pour remplir tout l'écran.Pourquoi l'ajout d'une vue à ma mise en page modifie-t-elle la largeur de la mise en page entière?

<?xml version="1.0" encoding="utf-8"?> 


<LinearLayout 
    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:layout_gravity="start" 
    android:orientation="vertical" 
    android:paddingLeft="6dp" 
    > 

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

     <TextView 
      android:id="@+id/text_label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="16dp" 
      android:textColor="@android:color/darker_gray" 
      android:textSize="12sp" 
      tools:text="Hello, Label" 
      android:paddingBottom="6dp" 
      android:paddingLeft="6dp"/> 

     <TextView 
      android:id="@+id/text_error" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="16dp" 
      android:textColor="@color/sr_red" 
      android:textSize="12sp" 
      tools:text="Error!"/> 
    </LinearLayout> 


<!-- this View causes the parent layout to fill the whole screen width--> 
    <View 
     android:layout_width="wrap_content" 
     android:layout_height="1dp" 
     android:layout_marginTop="2dp" 
     android:background="@android:color/darker_gray" 
     /> 


</LinearLayout> 

C'est le plus bas qui cause les problèmes.

Je en train de modifier la vue de cette façon:

val inputLayout = createLabelLayout(context, component.name, button) 
viewGroup.addView(inputLayout.root) 
inputLayout.setWidth(width) 

Et voici les classes appropriées

private fun <T : View> createLabelLayout(context: Context, text: String, child: T): LabelLayout<T> { 
    val layout = LabelLayout(context, child) 
    layout.label.text = text 
    return layout 
} 



private class LabelLayout<out T : View>(context: Context, child: T) { 
    val root = LayoutInflater.from(context).inflate(R.layout.item_custom_field_label, null, false) as ViewGroup 
    val label = root.findViewById(R.id.text_label) as TextView 
    val error = root.findViewById(R.id.text_error) as TextView 


    init { 
     root.addView(child, root.childCount - 1) 

    } 

    fun setWidth(width: Int) { 
     val params = root.layoutParams 
     params.width = width 
     root.layoutParams = params 
    } 
} 
+0

Les valeurs 'width' et' height' de votre racine 'LinearLayout' sont définies sur' match_parent'. Au moins c'est 'height' devrait être' wrap_content'. –

+0

À droite, mais je réinitialise la largeur de l'élément racine lorsque j'appelle setWidth(). Et la chose est si je commente la vue la plus basse dans le xml, (la vue qui est une ligne de séparation) alors l'élément de base met en forme à la bonne largeur. Il semble donc que ce soit la dernière vue de la mise en page qui cause les problèmes. –

Répondre

0

Ok, donc je fixe ce problème en changeant à la fois la LinearLayout racine et l'enfant LinearLayout à wrap_content. Avant je ne changeais que la largeur des mises en page parent.