2016-10-11 3 views
0

Ich habe Probleme beim Erstellen eines LinearLayout mit einem ImageView innerhalb programmatisch (und fügen Sie dieses LinearLayout zu einem anderen LinearLayout). Der XML-Code, den ich tryng bin neu zu erstellen, ist dies:Wie LinearLayout mit ImageView erstellen und es zu einem anderen LinearLayout programmatisch hinzufügen

   <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/linearLayout_morty_combinationsV" 
        android:gravity="center"> 

        <LinearLayout 
         android:orientation="vertical" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:gravity="center" 
         android:background="@color/black" 
         android:layout_weight="1"> 

         <ImageView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:src="@drawable/morty" 
          android:id="@+id/imageView1"/> 

        </LinearLayout> 

        <LinearLayout 
         android:orientation="vertical" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:gravity="center" 
         android:background="@color/black" 
         android:layout_weight="1"> 

         <ImageView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:src="@drawable/morty" 
          android:id="@+id/imageView2"/> 

        </LinearLayout> 
       </LinearLayout> 

Das ist mein Java-Code

 LinearLayout ll_combinations = (LinearLayout) findViewById(R.id.linearLayout_morty_combinationsV); 
     Iterator<MortyObj> iterator = mortys.iterator(); 


     while (iterator.hasNext()) { 
      final MortyObj mortyC = iterator.next(); 

      LinearLayout ll_new = new LinearLayout(this); 
      ll_new.setOrientation(LinearLayout.VERTICAL); 
      ll_new.setGravity(Gravity.CENTER); 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); 
      params.weight = 1f; 
      ll_new.setLayoutParams(params); 
      ll_new.setBackgroundColor(Color.WHITE); 


      ImageView morty_imageview = new ImageView(this); 

      try { 
       InputStream ims = getAssets().open("morty_images/" + mortyC.getId() + ".png"); 
       Drawable d = Drawable.createFromStream(ims, null); 
       morty_imageview.setImageDrawable(d); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      FrameLayout.LayoutParams imgParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); 
      morty_imageview.setLayoutParams(imgParams); 


      ll_new.addView(morty_imageview); 
      ll_combinations.addView(ll_new); 
      } 

Dies ist, wie es aussehen sollte:

XML

Und das So sieht es aus:

JAVA

Ich habe in den letzten 2 Stunden in diesem stecken.

+0

Warum nehmen Sie Frame Layout entfernen Sie diese Zeile. Weil ich deine XML-Datei sehe, solltest du die Bildansicht innerhalb des linearen Layouts haben wollen. In Ihrem Programm Fügen Sie einfach Ihre Bildansicht innerhalb des ll_new linearen Layouts und ll_new in den ll_combiations hinzu. –

+0

Entfernen Sie außerdem alles im übergeordneten Layout Ihrer XML-Datei. Weil du versuchst, das Programm nachzudenken. –

+0

Das Entfernen von imgParams ändert nichts. Ich habe imagwview zu ll_new und ll_new zu ll_combinations hinzugefügt, das sind die letzten beiden Zeilen. Ich habe auch das XML gereinigt, bevor ich etwas hinzugefügt habe. –

Antwort

0

Sie

ein Layout mit so etwas wie schaffen
View view = (View) findViewById(R.layout.current_layout); //the layout you set in `setContentView()` 
LinearLayout picLL = new LinearLayout(CurrentActivity.this); 
picLL.layout(0, 0, 100, 0); 
picLL.setLayoutParams(new LayoutParams(1000, 60)); 
picLL.setOrientation(LinearLayout.HORIZONTAL); 
((ViewGroup) view).addView(picLL); 

Was Sie in das Layout übergeben Parameter() wollen offensichtlich davon ab, was Sie wollen. Dann können Sie separate Ansichten erstellen, um sie zu dem gerade erstellten Layout hinzuzufügen. Aber ich empfehle sehr, durch die Dokumente zu lesen, um zu verstehen, was alles hier getan werden kann.

bearbeiten

ImageView myImage = new ImageView(this); 
picLL.addView(myImage); 
//set attributes for myImage; 
0

fand gerade das Problem. Wenn ich ein Bild von res/drawables lade bekomme ich die Ansicht, die ich will, wenn ich es von Assets laden funktioniert es nicht

Verwandte Themen