【问题标题】:Thumbnail does not fill_parent as expected缩略图未按预期填充父项
【发布时间】:2013-12-05 00:17:07
【问题描述】:

我试图让我的缩略图 (userVideoThumbImageView) 扩展到屏幕边缘,但它似乎没有这样做。我将宽度设置为 fill_parent 所以我不确定它为什么不这样做:

截图:

http://www.flickr.com/photos/110503723@N07/11207214295

list_item_user_video.xml:

   <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <com.blundell.tut.ui.widget.UrlImageView
        android:id="@+id/userVideoThumbImageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter"
        android:contentDescription="YouTube video thumbnail"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/userVideoTitleTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Video Title Not Found" />

</LinearLayout>

activity_main.xml:

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

    <com.blundell.tut.ui.widget.VideosListView
        android:id="@+id/videosListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="70dip"
        android:layout_alignParentBottom="true" >

        <HorizontalScrollView
            android:id="@+id/groupScrollView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" >
        </HorizontalScrollView>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/scroll_lt_arrow" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/scroll_rt_arrow" />

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

</RelativeLayout>

JAVA:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // If convertView wasn't null it means we have already set it to our list_item_user_video so no need to do it again
    if(convertView == null){
        // This is the layout we are using for each row in our list
        // anything you declare in this layout can then be referenced below
           convertView = mInflater.inflate(R.layout.list_item_user_video, parent, false); resolved the issue! 
    }
    // We are using a custom imageview so that we can load images using urls
    // For further explanation see: http://blog.blundell-apps.com/imageview-with-loading-spinner/
    UrlImageView thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);

    TextView title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView); 
    // Get a single video from our list
    final Video video = videos.get(position);
    // Set the image for the list item
    thumb.setImageDrawable(video.getThumbUrl());
    // Set the title for the list item
    title.setText(video.getTitle());

    return convertView;
}

【问题讨论】:

  • 你的意思是它不会拉伸屏幕的高度吗?
  • 不 - 我的意思是它不会拉伸宽度:flickr.com/photos/110503723@N07/11195356793
  • 尝试将 viewpager 更改为 match_parent?
  • 我做了 - 还是一样(如屏幕截图所示)
  • 注意 - 我不是想改变 viewpager - 我是想改变 userVideoThumbImageView

标签: android xml android-ui android-xml


【解决方案1】:

我认为图像现在显示为 100%。如果要将其拉伸到全宽,请将以下行添加到 urlImageView xml:

android:scaleType="fitCenter"

CenterInside 执行以下操作:均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)都等于或小于视图的相应尺寸(减去填充)。

查看documentation 了解有关缩放的更多信息。

【讨论】:

  • 导致相同的行为
  • 如果您有 4.3+,您可以在开发人员选项中启用名为“显示布局边界”的设置。这样你就可以看到ImageView是否太小,或者里面的图像。否则,您可以使用 hierarchyviewer 找出视图的边界。您可能会发现其中一个父视图太小了。
  • 啊,截图让我意识到我犯了一个错误,你可能应该使用 fitCenter。
  • 不确定你的意思.. 将 fill_parent 与 fitCenter 结合使用应该可以。
【解决方案2】:

在您的 getView 方法中,您要么需要手动将 LayoutParams 应用于膨胀视图,要么使用 inflate(myResource, parent, false) 而不是 inflate(myResource, null) 以防止您的 xml 布局参数被覆盖。在您的情况下,myResourceR.layout.list_item_user_video

然后就可以使用了

if (convertView==null)
    convertView = 
        mInflater.inflate(R.layout.list_item_user_video, parent, false);

编辑:

好的,看起来 XML 中的 ImageViews 无法自动缩放以适应特定宽度而不失真:https://stackoverflow.com/a/13925725/506796

因此,您必须在放大视图后缩放它们。首先,您需要知道图像的纵横比(宽度/高度)。由于您似乎具有恒定的纵横比,因此您可以将其设为静态的最终浮点变量,这样会容易得多。

然后您需要知道列表视图的宽度(以像素为单位)。同样,您很幸运,因为您的列表视图填满了屏幕的宽度。 (这更容易,因为测量列表视图的宽度会很棘手——你必须在布局完成第一遍之后再做。)你可以在适配器构造函数中使用

screenWidth = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getWidth()

您还可以创建一组布局参数以应用于所有图像视图。在您的适配器构造函数中:

LinearLayout.LayoutParams imageParams = new LayoutParams(screenWidth, (int)(screenWidth/aspectRaio));

您可以将这些参数应用于getView 中的图像视图。

【讨论】:

  • 您提供的答案给我留下了:LayoutInflater 类型中的方法 inflate(int, ViewGroup) 不适用于参数 (int, boolean)
  • 我建议使用三个参数的方法,你肯定错过了中间那个。
  • 是否转换视图 = mInflater.inflate(R.layout.activity_main, R.layout.list_item_user_video, false);你觉得对吗?
  • 我不确定 R.layout.activity_main 是否应该用于 myResource
  • 查看更新了解更多详情。您没有为参数输入正确的值。阅读 inflate 方法的文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多