Android在NetworkImageView XML中拉伸图像

时间:2015-04-11 18:20:18

标签: android xml image android-volley networkimageview

我正在试图弄清楚如何从TheTVDB(示例http://thetvdb.com/banners/graphical/84947-g6.jpg)获取电视横幅以适应屏幕宽度。这让我在那里,但我不想设置边距的空白区域:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/poster"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="8dp"
        android:scaleType="fitXY" />

</LinearLayout>

但是如果我删除了android:layout_margin="8dp"我突然只是加载了第一个横幅的一半,但没有其他横幅出现。

我已尝试将width和/或height设置为wrap_content并移除marginscaleType,这样可行,但是横幅不会在屏幕上伸展。

1 个答案:

答案 0 :(得分:0)

帖子HERE非常有帮助。他最终以解决方案从WindowManager获取屏幕宽度的解决方案非常有用。

结束了我这样做:

WindowManager mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display mDisplay = mWindowManager.getDefaultDisplay();
final int version = Build.VERSION.SDK_INT;
final int width;
if (version >= 13) {
    Point size = new Point();
    mDisplay.getSize(size);
    width = size.x;
} else {
    width = mDisplay.getWidth();
}
myPoster.setLayoutParams(new LinearLayout.LayoutParams(width, 199));