2016-07-21 38 views
1

我希望Imageview的寬度和高度匹配整個screen.After使用佈局Inflater來充氣視圖我設置佈局參數的view.But圖像的高度不覆蓋整個screen.Here是的代碼:設置LayoutParams編程不工作

LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View itemView = mLayoutInflater.inflate(R.layout.viewpager, container, false); 
    itemView.setLayoutParams(new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.MATCH_PARENT)); 

    ImageView imageView = (ImageView) itemView.findViewById(R.id.viewpager_image); 
    /*imageView.setLayoutParams(new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.MATCH_PARENT));*/ 
    imageView.setImageResource(mResources[position]); 
    container.addView(itemView); 
    return itemView; 

這樣做我的寬度是匹配的屏幕大小,但高度不匹配屏幕的屏幕布局我正在充氣具有線性佈局(寬度,高度 - MATCH_PARENT)和它的子圖像視圖(寬度,高度 - MATCH_PARENT)。我不知道我在這裏做了什麼錯誤,請幫助。謝謝。

+0

你檢查的ImageView的高度是否是恰當的屏幕尺寸和不是實際的圖像? 嘗試在圖像視圖上將scaleType設置爲FitXY? –

+0

@ Dr.aNdRO是的,它與設置scaleType ..謝謝 – andy

回答

1

在代碼中添加這些行

imageView.setScaleType(ScaleType.FIT_XY); 

imageView.setScaleType(ScaleType.CENTER); 

試試這個它可能工作

1
DisplayMetrics displaymetrics = new DisplayMetrics(); 
    //if in activity then use 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    //if in adapter then use 
    context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    //if in fragment then use 
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int width = displaymetrics.widthPixels; 
    int height = displaymetrics.heightPixels; 
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height); 
    imageView.setLayoutParams(layoutParams);