如何动态设定重力?
Bitmap bmp;
im = new ImageView (this);
bmp=getbmp(img);
im.setImageBitmap(bmp);
现在我想将图像放在顶部。我可以在main.xml中没有android:gravity
吗?
编辑:
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>
我有一个框架布局,我添加了图像,然后是文本,我希望图像位于屏幕顶部。
FrameLayout fm = new FrameLayout (this);
fm.addView(im);
fm.addView(fin); // fin = the text view
setContentView(fm);
答案 0 :(得分:28)
将ImageView.setScaleType()
与ImageView.ScaleType.FIT_START
一起使用作为值。
答案 1 :(得分:0)
您可以将ImageView放在LinearLayout中,并使用LinearLayout的setLayoutParams来设置布局的重力。
答案 2 :(得分:0)
如果可以帮助您,您可以尝试使用此代码 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文件
公共类GalleryListAdapter扩展了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;
}
答案 3 :(得分:0)
请参阅“http://developer.android.com/reference/android/widget/ImageView.ScaleType.html”。
答案 4 :(得分:0)
试试这个:
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);
请注意,您需要根据要设置的重力在FrameLayout.LayoutParams(宽度,高度,重力)中设置imageview的宽度和高度。