2016-03-01 116 views
0

我正在使用以下方法從Drawables動態創建Bitmaps並將它們賦給我的ImageViews。這種方法被重複調用。問題是,我無法正確設置圖像的大小,在不同的手機上它看起來不同,在我的Note 3上看起來很大,在我的朋友銀河核心上它看起來很小。動態設置ImageView的大小Android

方法

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> 

回答

相關問題