缩略图不FILL_PARENT预期 [英] Thumbnail does not fill_parent as expected

查看:99
本文介绍了缩略图不FILL_PARENT预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的缩略图(userVideoThumbImageView)延伸到屏幕的边缘,但它似乎并没有这样做。我有宽度设置为FILL_PARENT所以我不知道为什么它没有这样做的:

截图:

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

list_item_user_video.xml:

 < XML版本=1.0编码=UTF-8&GT?;

< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:重力=center_vertical
    机器人:方向=垂直>

    < com.blundell.tut.ui.widget.UrlImageView
        机器人:ID =@ + ID / userVideoThumbImageView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:scaleType =fitCenter
        机器人:contentDescription =的YouTube视频缩略图
        机器人:SRC =@可绘制/ ic_launcher/>

    <的TextView
        机器人:ID =@ + ID / userVideoTitleTextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=@机器人:彩色/黑白
        机器人:文本=视频标题未找到/>

< / LinearLayout中>
 

activity_main.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@机器人:彩色/白
    机器人:方向=垂直>

    < com.blundell.tut.ui.widget.VideosListView
        机器人:ID =@ + ID / videosListView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>

    < RelativeLayout的
        机器人:ID =@ + ID /页脚
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =70dip
        机器人:layout_alignParentBottom =真正的>

        < Horizo​​ntalScrollView
            机器人:ID =@ + ID / groupScrollView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =match_parent>
        < / Horizo​​ntalScrollView>

        <的ImageButton
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:SRC =@可绘制/ scroll_lt_arrow/>

        <的ImageButton
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:SRC =@可绘制/ scroll_rt_arrow/>

        < android.support.v4.view.ViewPager
            机器人:ID =@ + ID / view_pager
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent/>
    < / RelativeLayout的>

< / RelativeLayout的>
 

JAVA:

  @覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    //如果convertView不为null,则意味着我们已经将它设置为我们list_item_user_video所以没有必要再做了
    如果(convertView == NULL){
        //这是我们使用在我们的名单每一行的布局
        //任何你在这个布局声明就可以参考下面
           convertView = mInflater.inflate(R.layout.list_item_user_video,父母,假);解决了这个问题!
    }
    //我们使用自定义的ImageView,这样我们可以加载使用URL图片
    //为进一步说明,请参阅:http://blog.blundell-apps.com/imageview-with-loading-spinner/
    UrlImageView拇指=(UrlImageView)convertView.findViewById(R.id.userVideoThumbImageView);

    TextView的标题=(TextView中)convertView.findViewById(R.id.userVideoTitleTextView);
    //从我们的名单单一视频
    最终的视频视频= videos.get(位置);
    //设置图像列表项
    thumb.setImageDrawable(video.getThumbUrl());
    //设置列表项的标题
    title.setText(video.getTitle());

    返回convertView;
}
 

解决方案

在你的 getView 方法,您可能需要手动应用的LayoutParams的放大视图,或者使用膨胀(myResource,父母,FALSE)而不是膨胀(myResource,空)来prevent你的XML布局从PARAMS被覆盖。在你的情况, myResource R.layout.list_item_user_video

然后你可以用

 如果(convertView == NULL)
    convertView =
        mInflater.inflate(R.layout.list_item_user_video,父母,假);
 

编辑:

确定,它看起来像ImageViews不能在XML被自动缩放到适合一定的宽度不失真: http://stackoverflow.com /一/五十万六千七百九十六分之一千三百九十二万五千七百二十五

因此​​,您必须在充气后视图缩放它们。首先,你需要知道图像(宽/高)的纵横比。既然你似乎有一个恒定的高宽比,你可能只是使这个静态最终浮动变量,将容易得多。

然后,你需要知道的列表视图是以像素为单位有多宽。同样,你很幸运,因为你的ListView填满屏幕的宽度。 (这是比较容易的,因为这将是棘手的衡量ListView的宽度 - 你必须做的布局完成其第一轮后)你可以在屏幕宽度在适配器构造器

 屏幕宽度=((窗口管理器)context.getSystemService(Context.WINDOW_SERVICE))。的getWidth()
 

您也可以创建一组布局PARAMS的适用于所有图像的意见。在您的适配器的构造:

  LinearLayout.LayoutParams imageParams =新的LayoutParams(屏幕宽度,(INT)(屏幕宽度/ aspectRaio));
 

您可以在 getView 将这些PARAMS到图像视图。

I'm attempting to get my thumbnail (userVideoThumbImageView) to extend to the edge of the screen but it does not seem to do so. I have the width set to fill_parent so I'm not sure why it isn't doing so:

Screenshot:

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;
}

解决方案

In your getView method, you either need to manually apply LayoutParams to the inflated view, or else use inflate(myResource, parent, false) instead of inflate(myResource, null) to prevent your xml layout params from being overwritten. In your case, myResource is R.layout.list_item_user_video.

Then you can use

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

EDIT:

OK, it looks like ImageViews cannot in XML be automatically scaled to fit a certain width without distortion: http://stackoverflow.com/a/13925725/506796

Therefore, you must scale them after inflating the view. First you need to know the aspect ratio of the image (width/height). Since you seem to have a constant aspect ratio, you could just make this a static final float variable which will be much easier.

Then you need to know how wide the listview is in pixels. Again, you're in luck since your listview fills the width of the screen. (This is easier, because it would be tricky to measure the listview's width--you would have to do it after layout completes its first pass.) You can get the screen width in the adapter constructor with

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

You can also create a single set of layout params to apply to all of your image views. In your adapter constructor:

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

You can apply these params to the image view in getView.

这篇关于缩略图不FILL_PARENT预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆