我使用以下方法从Drawables动态创建Bitmaps
并将它们设置为我的ImageViews。重复调用此方法。问题是我无法正确设置图像的大小,在不同的手机上它看起来不同,在我的Note 3看起来很大,在我的朋友galaxy核心看起来很小。
方法
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);
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>
答案 0 :(得分:0)
: http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
CENTER_INSIDE均匀缩放图像(保持图像的外观 比率),以便图像的尺寸(宽度和高度)都是 等于或小于视图的相应尺寸(减去 填充)。然后图像在视图中居中。