2017-07-05 1 views
0

J'ai une disposition linéaire simple et je veux diviser l'écran en deux parties de taille égale.Ajuster la taille de la mise en page parent

Dans le supérieur, je veux mettre un SurfaceView qui a exactement la même taille que son parent, c'est-à-dire la moitié de l'écran. Malheureusement, il faut toujours soit prendre tout l'écran ou rien (selon la configuration). Pourquoi est-ce et comment puis-je le changer?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="2" 
    android:orientation="vertical"> 

    <LinearLayout android:id="@+id/layoutUpperDaw" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#192832"> 

     <com.example.MySurvfaceView 
      android:id="@+id/id_mySurfaceView" 
      android:layout_width="match_parent" 

      android:layout_height="0pt" 
      android:layout_weight="1" 

      OR 

      android:layout_height="match_parent" 

      > 

     </com.example.MySurvfaceView> 

    </LinearLayout> 

    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#193222"> 

     ... 
    </LinearLayout> 
</LinearLayout> 

Répondre

1

Lorsque vous fournissez weight vous devez définir la hauteur ou le poids à 0DP comme ci-dessous.

<LinearLayout android:id="@+id/layoutUpperDaw" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#192832"> 

et

<com.example.MySurvfaceView 
      android:id="@+id/id_mySurfaceView" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      > 

Mise à jour: réponse ci-dessus est correct sur l'utilisation du poids, assurez-vous que soit la largeur ou hauteur est 0DP à tous les endroits où vous avez utilisé le poids.

Vous n'avez pas vraiment besoin de LinearLayout imbriqué. Suivant devrait fonctionner.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


     <com.example.MySurvfaceView 
      android:id="@+id/id_mySurfaceView" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      > 

     </com.example.MySurvfaceView> 



    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#193222"> 


    </LinearLayout> 
</LinearLayout> 
+0

Ne fonctionne pas pour moi. Le 'MySurfaceView' n'est pas visible alors. – user1225905

+0

@ user1225905 Voir la mise à jour – Sharj

+0

Ok, l'approche sans le premier interne imbriqué 'LinearLayout' fonctionne, mais je ne suis pas sûr d'avoir la logique derrière le LinearLayout. Quand j'ajoute un 'LinearLayout' avec' weight = 1' et 'height = 0' autour de' MySurvfaceView' ça ne marche pas. – user1225905

0

Programmatically:

  1. Trouver la mise en page de parent.

    GridLayout GridLayout = (GridLayout) findViewById (R.id.gridLayout); `

  2. Obtenir mesurer la hauteur et la largeur du parent.

    int height = gridLayout.measuredHeight();

    int largeur = gridLayout.measuredWidth();

3.Calculate hauteur de vue des enfants et par exemple width.For deux enfants prenant chacun 50% de la hauteur du parent mais en largeur de parent:

int childHeight = height/2; 
int childWidth = width; 

4.Get instanceof LayoutParams selon type parent.

GridLayout.LayoutParams params = new GridLayout.LayoutParams(); 

5. Réglez la hauteur et la largeur sur les paramètres de disposition.

params.width = childWidth; 
params.height = childHeight; 

6.Créez une vue enfant et définissez les paramètres de mise en page.

ImageView imageview = new ImageView(); 
imageview.setLayoutParams(params); 

7.Ajouter l'enfant Voir au parent.

gridLayout.addView(ImageView);