2014-01-07 32 views
5

こんにちは、私は次のXML持っている:私はやりたい何線形レイアウトでプログラム的に配置位置を移動する

<?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> 

はプログラムで私のTextMessageの左側に私のImageViewの変化の位置を持っていることですが。

これは、ImageViewをTextViewの上に置くことでxmlで行うことができます。しかし、私はプログラム的に同じ結果を達成したいと思います。

私は

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

Try LayoutParams。 –

答えて

7

は、それは何の効果も示さない。このライン

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

を使用することにより

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

....このようにしてみてください。ここでは、直線レイアウトに重力を適用しているので、この重力はコンテンツ全体を左右に整列させます。

+0

私はaddView(int、view、some_other_param)を試しています。 – Jeremy

+0

よろしくお願いしますyourLinearLayout.addView(view、index)Thx 。私は今あなたの答えを受け入れるでしょう。 – Jeremy

+0

何らかの方法..あなたはそれを楽しむことができます。 – saa

0

http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

私のアダプタでこれを持っていると仮定は、特定のコンポーネントのレイアウトパラメータを設定する方法についての詳細は、上記のドキュメントを参照してください。

LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
        R.id.componentid; 

btn.leftMargin = x; 
btn.topMargin = y; 
btn.width = width; 
btn.height = height; 

imageview.setLayoutParams(layoutParam); 

上記のコードでは、layoutparamの使用方法を示します。

関連する問題