2017-08-14 3 views
0

Je veux charger des vues personnalisées dans mon RelativeLayout mais je ne sais pas comment. Le code que j'ai essayé ne fonctionne pas, donc quelqu'un sait-il comment le faire correctement?Comment charger une classe Java View personnalisée à un RelativeLayout par programmation

XML

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin"> 

     <TextView 
      android:id="@+id/textView0" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_marginBottom="20dp" 
      style="@android:style/TextAppearance.Medium" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_marginBottom="2dp" 
      style="@android:style/TextAppearance.Medium" 
      android:layout_below="@id/textView0" /> 
     <View 
      android:id="@+id/drawing0" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_below="@id/textView1" /> 
     <View 
      android:id="@+id/drawing1"     
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_marginTop="2dp" 
      android:layout_below="@id/drawing0" /> 
     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@android:style/TextAppearance.Large" 
      android:layout_below="@id/drawing1" /> 
     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@android:style/TextAppearance.Large" 
      android:layout_marginStart="10dp" 
      android:layout_below="@id/drawing1" 
      android:layout_toEndOf="@id/textView2" /> 
     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@android:style/TextAppearance.Medium" 
      android:layout_below="@id/textView3" /> 
     <TextView 
      android:id="@+id/textView5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@android:style/TextAppearance.Medium" 
      android:layout_marginBottom="30dp" 
      android:layout_below="@id/textView4" /> 
    </RelativeLayout> 

</ScrollView> 

Shapes.java

public class Shapes extends android.support.v4.app.Fragment { 

    public Shapes() { 
    } 

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

     return inflater.inflate(R.layout.shapes, container, false); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     View v = getView(); 
     assert v != null; 

     //blue shape drawing 
     View cv0 = (View)v.findViewById(R.id.drawing0); 
     v.addView(new BlueShape(getActivity())); 

     //green shape drawing 
     View cv1 = (View)v.findViewById(R.id.drawing1); 
     v.addView(new GreenShape(getActivity())); 

     super.onActivityCreated(savedInstanceState); 
    } 
} 

BlueShape.java

public class BlueShape extends View { 
    private final Paint mBlue = new Paint(); 

    ... 

} 

GreenShape.java

public class GreenShape extends View { 
    private final Paint mGreen = new Paint(); 

    ... 

} 
+0

Quel est le problème? – Vyacheslav

+0

Où l'erreur apparaît? – Vyacheslav

Répondre

1

se réfèrent à https://developer.android.com/guide/topics/ui/custom-components.html

spécifiquement sous

Modification d'une vue existante Type de

  1. Utilisez le composant personnalisé

vous pouvez changez le xml dans votre RelativeLayout

<View... 

à

<your.package.GreenShape... 

si vous ne savez pas à l'avance ce que la vue va être, changer ensuite à

<FrameLayout... instead of <View... 

, puis ajoutez l'enfant (GreenShape ou BlueShape) programme à cette mise en page

+0

Je l'ai déjà fait mais j'ai besoin de réutiliser la mise en page pour montrer des dizaines d'autres vues différentes, ce qui fait que ce n'est pas une bonne idée. – MacaronLover

+0

@MacaronLover \t change-t-il de drawing0 et drawing1 de View à FrameLayout? vous avez besoin d'une mise en page pour contenir les vues enfant. aussi le BlueShape/GreenShape peut avoir la largeur et la hauteur de 0dp (d'où il n'apparaît pas) –

1

CV0, CV1 ne sont pas ViewGroup. Essayez de remplacer cv0, cv1 par ViewGroup et déplacez le code d'onActivityCreated vers la méthode de rappel onViewCreated.

voici le site de référence. android/view/ViewGroup

1

Je pense que le problème est que vous utilisez v.addView() au lieu de

cv0.addView() 

Mais je suggère d'utiliser les éléments suivants au lieu de votre View:

<LinearLayout 
      android:id="@+id/drawing1"     
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_marginTop="2dp" 
      android:layout_below="@id/drawing0" /> 

puis, à l'intérieur du Code Java:

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    LinearLayout cv0 = (LinearLayout)getView().findViewById(R.id.drawing0); 
    cv0.addView(new BlueShape(getActivity()), 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); 
+0

@MacaronLover code incorrect.J'ai corrigé – Vyacheslav