14

重力を動的に設定するにはどうすればよいですか?

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

次に、画像を一番上に配置します。android:gravitymain.xmlなしでこれを行うことはできますか?

編集 :

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);
4

5 に答える 5

31

値として使用ImageView.setScaleType()ImageView.ScaleType.FIT_STARTます。

于 2011-10-03T07:15:59.350 に答える
0

ImageViewをLinearLayout内に配置し、LinearLayoutのsetLayoutParamsを使用してレイアウトの重力を設定できます。

于 2011-10-03T07:21:30.227 に答える
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(width、height、gravity)でimageviewの幅と高さを設定する必要があることに注意してください。

于 2011-10-03T07:49:05.890 に答える
0

「http://developer.android.com/reference/android/widget/ImageView.ScaleType.html」を参照してください。

于 2011-10-03T07:38:01.647 に答える
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 ファイル

public class 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;

}
于 2011-10-03T07:26:12.303 に答える