2014-01-07 3 views
5

Hallo ich habe folgendes xml:Verschieben von Layout-Position programmatisch in linearem Layout

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

    <LinearLayout 
     android:id="@+id/wrapper" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_margin="10dip" 
     android:weightSum="1" 
     android:background="#000" 
     android:orientation="horizontal"> 
     <TextView 
      android:id="@+id/textMessage" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight = ".9" 
      android:layout_gravity="center" 
      android:layout_margin="5dip" 
      android:paddingLeft="10dip" 
      android:background="#FFF" 
      android:text="TEXT NOT SET" 
      android:textColor="@android:color/white" /> 
     <ImageView 
      android:id="@+id/thumbPhoto" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight = ".1" 
      android:src="@drawable/ic_contact_default"/> 
    </LinearLayout> 
</LinearLayout> 

Was ich tun möchte, ist die Position meiner Imagewechsel programmatisch auf die linke Seite meines Textm haben.

Dies könnte durch XML geschehen, indem ich meine ImageView oberhalb der TextView platzieren. Allerdings möchte ich das gleiche Ergebnis aber programmatisch erreichen.

Angenommen, ich habe in meinem Adapter

public View getView(int position, View convertView, ViewGroup parent) { 


     View row = convertView; 
     if (row == null) { 
       LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       row = inflater.inflate(R.layout.chat_item, parent, false); 
     } 

     wrapper = (LinearLayout) row.findViewById(R.id.wrapper); 

     // How should i change the position of my ImageView ?? 
     wrapper.setGravity([some_condition] ? Gravity.LEFT : Gravity.RIGHT); 

     return row; 
} 
+0

versuchen LayoutParams. –

Antwort

7

versuchen, wie diese ....

ImageView imageView= (ImageView)yourLinearLayout.findViewById(R.id.thumbPhoto); 
yourLinearLayout.removeViewAt(1); 
yourLinearLayout.addView(0, imageView); 

Durch diese Zeile mit

// How should i change the position of my ImageView ?? 
     wrapper.setGravity([some_condition] ? Gravity.LEFT : Gravity.RIGHT); 

Es zeigt keine Wirkung. Da hier Schwerkraft auf lineares Layout angewendet wird, richtet diese Gravitation ihren gesamten Inhalt entweder nach links oder nach rechts aus.

+0

danke für den Vorschlag, aber ich konnte es nicht mit addViewAt (int, anzeigen) Ich versuche jedoch mit addView (int, view, some_other_param) – Jeremy

+0

Yup, es ist wahrscheinlich, dass yourLinearLayout.addView (Ansicht, Index) Thx sein . ich werde jetzt deine antwort annehmen .. – Jeremy

+0

egal .. du hast es .. geniesse. – saa