问题
How can I dynamically set the gravity ?
Bitmap bmp;
im = new ImageView (this);
bmp=getbmp(img);
im.setImageBitmap(bmp);
Now I would like to put the image in the top . Ho can I do this without android:gravity
in 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>
I have a frame layout and I add to it the image , and then the text and I would like the image to be on the top of the screen.
FrameLayout fm = new FrameLayout (this);
fm.addView(im);
fm.addView(fin); // fin = the text view
setContentView(fm);
回答1:
Use ImageView.setScaleType()
with ImageView.ScaleType.FIT_START
as value.
回答2:
You can put ImageView inside a LinearLayout and the use setLayoutParams of the LinearLayout to set gravity of the layout.
回答3:
You can try this code if may be help you xml file
<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 file
public class GalleryListAdapter extends 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;
}
回答4:
See "http://developer.android.com/reference/android/widget/ImageView.ScaleType.html".
回答5:
Try this:
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);
Please note that you need to set width and height of imageview in FrameLayout.LayoutParams(width,height,gravity) according to what gravity you want to set.
来源:https://stackoverflow.com/questions/7631983/android-image-view-gravity