如何动态控制ImageView Gravity

时间:2014-06-24 18:03:23

标签: android android-linearlayout

我有一个像这样的ListView:

 <?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="wrap_content" >

  <ImageView
    android:id="@+id/MSGimage"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <TextView
        android:id="@+id/MSGname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="Hello bubbles!"
        android:textColor="@android:color/primary_text_light" />

</LinearLayout>

</LinearLayout>

并用我控制子LinearLayout:

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


    theMessage = (TextView) row.findViewById(R.id.comment);
    theName = (TextView) row.findViewById(R.id.MSGname);
    theImage = (ImageView)row.findViewById(R.id.MSGimage);

     String Namer= 
 mCursor.getString(mCursor.getColumnIndex(MyDbHelperSinglechat.COL_USERNAME));
           String namer[] = Namer.split("\\@");
           String imagenamer=namer[0];

    theName.setText(imagenamer);

theMessage.setText(mCursor.getString(mCursor.getColumnIndex
(MyDbHelperSinglechat.COL_MESSAG
 E)));

    int isright= 
 Integer.valueOf(mCursor.getString(mCursor.getColumnIndex("isright")));

    theMessage.setBackgroundResource(isright==0 ? R.drawable.bubble_yellow : 
 R.drawable.bubble_green);
    wrapper.setGravity(isright==0 ? Gravity.LEFT : Gravity.RIGHT);
    theName.setGravity(isright==0 ? Gravity.LEFT : Gravity.RIGHT);


    File file= new File("storage/sdcard/LifeMatePrivate/ProfileImage
 /ProfileImage,imagechange_2,"+imagenamer+",.jpg");
    if(file.exists()){
    Picasso.with(mContext).load(new File("storage/sdcard/LifeMatePrivate  
/ProfileImage
/ProfileImage,imagechange_2,"+imagenamer+",.jpg")).resize(100,100
).centerCrop().into(theIma    ge);
    }else{
    Picasso.with(mContext).load(new File("storage/sdcard/LifeMatePrivate 
 /ProfileImage/Default.jpg")).resize(100,100).centerCrop().into(theImage);
    }

问题是,ImageView默认位于View的左侧,但我想当重力是Right时,它会移动到TextView的右侧。你可以帮我实现吗?非常感谢。

0 个答案:

没有答案