Android 动态设置ImageView的大小

Android 动态设置ImageView的大小,android,layout,imageview,Android,Layout,Imageview,我正在使用以下方法从Drawables动态创建位图,并将其分配到我的ImageView。重复调用此方法。问题是我无法正确设置图像的大小,在不同的手机上它看起来不同,在我的笔记3上它看起来很大,在我的朋友galaxy core上它看起来很小 方法 public void setImageView(int i,Integer d, LinearLayout layout ) { ImageView imageView = new ImageView(this); imageView

我正在使用以下方法从Drawables动态创建
位图
,并将其分配到我的ImageView。重复调用此方法。问题是我无法正确设置图像的大小,在不同的手机上它看起来不同,在我的笔记3上它看起来很大,在我的朋友galaxy core上它看起来很小

方法

 public void setImageView(int i,Integer d, LinearLayout layout ) {
    ImageView imageView = new ImageView(this);
    imageView.setId(i);
    imageView.setPadding(2, 2, 2, 2);
    imageView.setMaxHeight(200);
    imageView.setMaxWidth(200);
    imageView.setAdjustViewBounds(true);
    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    parms.setMargins(15, 50, 15, 25);
    imageView.setLayoutParams(parms);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), d));
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    layout.addView(imageView);
}
方法的使用

horizontal_scroll.setVisibility(View.VISIBLE);
    LinearLayout layout = (LinearLayout) findViewById(R.id.scroll_view_layout);
                setImageView(1, R.drawable.hex_filled, layout);
                setImageView(2, R.drawable.hex_filled, layout);
    setImageView(3, R.drawable.hex_filled, layout);
    setImageView(4, R.drawable.hex_filled, layout);
    setImageView(5, R.drawable.hex_filled, layout);
    setImageView(6, R.drawable.hex_filled, layout);
    setImageView(7, R.drawable.hex_filled, layout);
布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
android:orientation="vertical"
android:id="@+id/main_layout"
android:clipChildren="false"
android:weightSum="100"
tools:context=".City_Search">
<HorizontalScrollView
    android:id="@+id/horizontal_scroll_view"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:background="@drawable/white_lines"
    android:layout_weight="15"
    android:layout_height="0dp" >

    <LinearLayout
        android:id="@+id/scroll_view_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    </LinearLayout>

</HorizontalScrollView>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="85"
    android:weightSum="100"
    android:id="@+id/linear_layout_listview"
    android:orientation="vertical"
   >
    <ListView
        android:layout_width="fill_parent"
        android:layout_weight="90"
        android:id="@+id/followed_cities"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:layout_height="0dp"></ListView>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_weight="10"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:id="@+id/scroll_up"
        android:gravity="center"
        android:layout_height="0dp">
        <ImageView
            android:layout_width="30dp"
            android:src="@drawable/hex_filled"
            android:layout_height="30dp" />
    </LinearLayout>
</LinearLayout>


您尝试过不同的刻度类型吗

来自谷歌文档:

居中\u均匀缩放图像(保持图像的外观 比率),这样图像的两个尺寸(宽度和高度)都将 等于或小于视图的相应尺寸(减去 填充)。然后图像在视图中居中