2011-10-03 16 views
12

¿Cómo puedo establecer dinámicamente la gravedad?Android image view gravity

Bitmap bmp; 
im = new ImageView (this); 
bmp=getbmp(img); 
im.setImageBitmap(bmp); 

Ahora me gustaría poner la imagen en la parte superior. ¿Puedo hacerlo sin android:gravity en main.xml?

EDIT:

main.xml

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

    > 
</ScrollView> 

tengo un diseño de marcos y agrego a ella la imagen, y luego el texto y me gusta la imagen de estar en la parte superior de la pantalla.

FrameLayout fm = new FrameLayout (this); 
fm.addView(im); 
fm.addView(fin); // fin = the text view 
setContentView(fm); 
+0

Más información, por favor. Publica tu archivo de diseño. –

+0

hecho la edición. – Teo

+0

¿Hay alguna razón por la que no solo coloque ImageView en el archivo de diseño? –

Respuesta

27

Uso ImageView.setScaleType() con ImageView.ScaleType.FIT_START como valor.

+0

im.setScaleType (im.FIT_START); ? no funciona – Teo

+0

Lo siento, es así: im.setScaleType (ImageView.ScaleType.FIT_START); –

+0

Gracias, funciona, pero ¿por qué pone el texto sobre la imagen? Que puedo hacer ? – Teo

0

Puede colocar ImageView dentro de LinearLayout y el uso setLayoutParams de LinearLayout para establecer la gravedad del diseño.

0

Puede probar este código si le pueden estar ayudará archivo XML

<GridView android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:id="@+id/gallerylistactivity_gallery" 
    android:columnWidth="90dp" android:numColumns="4" 
    android:verticalSpacing="5dp" android:horizontalSpacing="10dp" 
    android:gravity="center" android:stretchMode="spacingWidth"/> 

.java presentar

GalleryListAdapter clase pública se extiende BaseAdapter {

private Context mContext; 

private Integer[] mImageIds = { R.drawable.gallery_01, 
     R.drawable.gallery_02, R.drawable.gallery_03, 
     R.drawable.gallery_04, R.drawable.gallery_05, 
     R.drawable.gallery_06, R.drawable.gallery_07, 
     R.drawable.gallery_08, R.drawable.gallery_10, 
     R.drawable.gallery_09 }; 

public GalleryListAdapter(Context c) { 
    this.mContext = c; 

} 

@Override 
public int getCount() { 
    return mImageIds.length; 

} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

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

    ImageView imageView = new ImageView(mContext); 
    imageView.setImageResource(mImageIds[position]); 
    imageView.setLayoutParams(new GridView.LayoutParams(100, 100)); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    imageView.setPadding(3, 3, 3, 3); 

    return imageView; 

} 
0

Ver "http: // developer.android.com/reference/android/widget/ImageView.ScaleType.html ".

0

Prueba esto:

Bitmap bmp;  
ImageView img= new ImageView (this);  
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.icon);  
img.setImageBitmap(bmp);    
img.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,      
        LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL)); 
mLinearLayout.addView(img); 

Tenga en cuenta que es necesario establecer la anchura y la altura de imageview en FrameLayout.LayoutParams (anchura, altura, peso) de acuerdo con la gravedad de lo que desea establecer.