ImageView在横向模式下

时间:2014-06-23 06:11:28

标签: android android-layout imageview

更改为横向模式时,显示imageView时出现问题。

首先,我在我的布局中使用了ImageView。

<ImageView
    android:id="@+id/img_gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/img_des"
    android:scaleType="fitcenter"/>

它在横向和纵向模式下都能正常工作。但Imageview没有填充布局。 所以我添加了一行布局来显示填充图像(android:adjustViewBounds =&#34; true&#34;)

<ImageView
    android:id="@+id/img_gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/img_des"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"/>

它在肖像方面工作得很好,但横向只显示一个空白。

其次,我尝试使用ResizableImageView。

public class ResizableImageView extends ImageView {

    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable drawable = getDrawable();
        if (drawable != null) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int diw = drawable.getIntrinsicWidth();
            if (diw > 0) {
                int height = width * drawable.getIntrinsicHeight() / diw;
                setMeasuredDimension(width, height);
            } else
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        } else
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

,布局为:

<com.ourteamapp.others.ResizableImageView
    android:id="@+id/imdg_gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/img_des"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"/>

还有同样的问题。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

请使用GridView,它会起作用。

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

主要活动

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

图像适配器

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

来源http://developer.android.com/guide/topics/ui/layout/gridview.html#example