2012-08-14 2 views
2

Actuellement je suit la mise en page:Créer une bordure autour d'une vue personnalisée centrée et supprimer LinearLayout inutile?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/editorRootView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <RelativeLayout android:id="RL1" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1"> 
     <!-- LinearLayout needed so we have an border outside of the EditorView --> 
     <LinearLayout android:id="LL1" 
      android:background="@drawable/border" 
      android:layout_centerInParent="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <xenolupus.EditorView 
       android:id="@+id/editorView" 
       android:layout_width="300dip" 
       android:layout_height="216dip" /> 
     </LinearLayout> 
    </RelativeLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dip" 
     android:orientation="horizontal" > 
     <Button 
      android:id="@+id/otherImage" 
      android:layout_width="150dip" 
      android:layout_height="60dip" 
      android:text="@string/cardEditor_OtherImageButtonText" /> 
     <Button 
      android:id="@+id/next" 
      android:layout_width="150dip" 
      android:layout_height="60dip" 
      android:text="@string/cardEditor_NextButtonText" /> 
    </LinearLayout> 
</LinearLayout> 

Et utilisé @ drawable/frontière:

<?xml version="1.0" encoding="UTF-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid 
     android:color="#FFFFFF" /> 
    <stroke 
     android:width="10dip" 
     android:color="#FF0099CA" /> 
    <padding 
     android:left="10dip" 
     android:top="10dip" 
     android:right="10dip" 
     android:bottom="10dip" /> 
    <corners 
     android:radius="5dip" /> 
</shape> 

Cependant Eclipse me met en garde (! Triangle jaune avec) que le LinearLayout LV1 dans le RelativeLayout RL1 est inutile et doit être supprimé:

Cette disposition LinearLayout ou son parent RelativeLayout est inutile; transfère l'attribut d'arrière-plan à l'autre vue.

Comme le RelativeLayout est nécessaire pour centrer le EditorView J'ai essayé de retirer le LinearLayout LV1 et en ajoutant l'androïde: arrière-plan de la LinearLayout LV1 au EditorView. Cependant, cela permet à la bordure de disparaître derrière le contenu de EditorView.

Existe-t-il un autre moyen d'ajouter une bordure en dehors de l'éditeur ou dois-je simplement ignorer l'avertissement?

Salutations Xeno Lupus

Répondre

1
yes it's right, put the background inside your <xenolupus.EditorView 
     like this  
<xenolupus.EditorView 
        android:background="@drawable/border" 
        android:padding="10dp" 
        android:id="@+id/editorView" 
        android:layout_width="300dip" 
        android:layout_height="216dip" /> 

    and then add gravity to it parent the RL1 layout 

    > android:gravity="center" 
0

View peut tirer arrière-plan par défaut. Si vous avez développé vous EditorView correctement, vous pouvez simplement mettre en arrière-plan et rembourrages directement en XML:

<xenolupus.EditorView 
    android:id="@+id/editorView" 
    android:layout_width="300dip" 
    android:layout_height="216dip" 
    android:background="@drawable/border" 
    android:padding="10dp" /> 

Par « développé correctement » Je veux dire que votre point de vue calculer sa largeur en hauteur (en onMeasure ou onSizeChanged méthodes) avec paddings. En d'autres termes: lors du calcul de la taille, vous avez utilisé les méthodes getPadding*().

NOTE

Ne pas oublier d'appeler super.onDraw()!

Questions connexes