【问题标题】:match_parent and does not fill as expected - Android / XMLmatch_parent 并没有按预期填充 - Android / XML
【发布时间】:2013-12-19 17:03:18
【问题描述】:

我在 listView 中有一个 YoutubeThumbnail/ImageView,我想填充/拉伸到屏幕边缘:

        <com.example.project.ui.widget.VideosListView
            android:id="@+id/videosListView"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        </LinearLayout>

 <com.example.project.ui.widget.UrlImageView
        android:id="@+id/userVideoThumbImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black"
        android:scaleType="fitXY"
        android:clickable="false"
        android:contentDescription="YouTube video thumbnail"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center"
        android:src="@drawable/ic_launcher" />

目前它似乎没有这样做,并且在某些设备上,缩略图看起来很小。

HTC 蝴蝶截图:

Galaxy S2 截图:

我不确定我到底做错了什么 - 感谢任何输入。

home.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/darkgrey"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:background="#AAFFFFFF" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:orientation="vertical" >

                 <com.example.project.ui.widget.VideosListView
            android:id="@+id/videosListView"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <RelativeLayout
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:layout_alignParentBottom="true" >

                <ImageButton
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:background="@color/darkgrey"
                    android:scaleType="centerCrop"
                    android:src="@drawable/home_up_btn" />
            </RelativeLayout>

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

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

                <ImageButton
                    android:layout_width="30dip"
                    android:layout_height="30dip"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="15dp"
                    android:focusable="false"
                    android:src="@drawable/scroll_lt_arrow" />

                <ImageButton
                    android:layout_width="30dip"
                    android:layout_height="30dip"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="15dp"
                    android:focusable="false"
                    android:src="@drawable/scroll_rt_arrow" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

         <com.example.project.ui.widget.UrlImageView
        android:id="@+id/userVideoThumbImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black"
        android:scaleType="fitXY"
        android:clickable="false"
        android:contentDescription="YouTube video thumbnail"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center"
        android:src="@drawable/ic_launcher" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:visibility="invisible" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/userVideoTitleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            android:text="Video Title Not Found"
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <Button
            android:id="@+id/fav_up_btn1"
            android:layout_width="27dp"
            android:layout_height="27dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/fav_up_btn1"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="right"
            android:paddingRight="5dp"
            android:paddingTop="5dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/userVideouploaderTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            android:textColor="@color/verylightgrey"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/userVideoviewsTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/userVideouploaderTextView"
            android:textColor="@android:color/black"
            android:textSize="16sp" />
    </RelativeLayout>

</LinearLayout>

JAVA:

private VideosListView listView;


listView = (VideosListView) findViewById(R.id.videosListView);
        listView.setOnVideoClickListener(this);


            private void populateListWithVideos(Message msg) {
        Library lib = (Library) msg.getData().get(
                GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());

    }

编辑:

<?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:orientation="vertical" >

    <com.idg.omv.ui.widget.UrlImageView
        android:id="@+id/userVideoThumbImageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="false"
        android:background="@android:color/black"
        android:clickable="false"
        android:contentDescription="YouTube video thumbnail"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:visibility="invisible" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

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

        <Button
            android:id="@+id/fav_up_btn1"
            android:layout_width="27dp"
            android:layout_height="27dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/fav_up_btn1"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="right"
            android:paddingRight="5dp"
            android:paddingTop="5dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/userVideouploaderTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            android:textColor="@color/verylightgrey"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/userVideoviewsTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/userVideouploaderTextView"
            android:textColor="@android:color/black"
            android:textSize="16sp" />
    </RelativeLayout>

</LinearLayout>

编辑后的截图:

【问题讨论】:

  • 这些设备是什么 API 版本?在版本 18 (=4.3) 之前,RelativeLayout 中存在测量错误
  • 他们都在使用 4.2.2 或更低版本(知道解决方法/修复可能是什么吗?)
  • 不应该用match_parent代替fill_parent吗?
  • 在 XML 中的哪一点? (我在哪里都在使用 fill_parent?)
  • @user3113035 你试过下面的代码吗?

标签: android xml thumbnails android-ui android-xml


【解决方案1】:

如果您正在扩展 ImageView,请尝试应用此功能:

android:scaleType="fitXY"

或在javacode中

imageView.setScaleType(ScaleType.FIT_XY);

2) 应用上面提到的代码:

<com.example.project.ui.widget.UrlImageView
        android:id="@+id/userVideoThumbImageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="false"
        android:background="@android:color/black"
        android:clickable="false"
        android:contentDescription="YouTube video thumbnail"
        android:focusable="false"
        android:scaleType="fitXY"
        android:focusableInTouchMode="false"
        android:gravity="center"
        android:src="@drawable/ic_launcher" />

编辑已更改:

android:adjustViewBounds="true"

android:adjustViewBounds="false"

【讨论】:

  • @user3113035 尝试将 android:adjustViewBounds="true" 更改为 android:adjustViewBounds="false"
  • 不完全解决我的问题,但我发现了一些有用的东西!
【解决方案2】:

UrlImageView 既然是从网络上获取图片的,那么它在获取图片之前怎么知道它的尺寸应该是多少呢?

您必须先确定 listView 项目的视图高度,然后才能显示它们。

还有 android:adjustViewBounds="true" 不能很好地与 android:scaleType="fitXY" 混合,因为 adjustViewBounds 说它会保持纵横比(链接here),而 fitXY 说它不会(链接here)。你需要决定什么对你很重要——拉伸图像或保持纵横比。这个问题无处不在,不仅在安卓上。

由于您似乎是列表视图的新手,我建议您观看this lecture

另外,顺便说一句,我在屏幕截图上看到的看起来不像是一个好的 android 应用程序设计。请查看this link。如果您的应用不遵循 google 的指导方针,即使它很受欢迎,它在 Play 商店中获得推荐的机会也非常低。

【讨论】:

  • 增加图像的大小比保持纵横比更重要 - 找到一种这样做的方法将解决我此时的主要问题(我将根据android 指南,但此时我真的需要想办法让这张图片变大)
  • 我用来构建部分内容的文章的作者似乎认为 scaleType=fitXY 应该工作 - 但它似乎没有(看看这篇文章中的第一条评论)blog.blundell-apps.com/show-youtube-user-videos-in-a-listview
  • 所以你不关心拉伸?如果是这样,请使用 adjustViewBounds="false" , scaleType="fitXY" ,并移除重力。
  • 看,我不知道这个名为“UrlImageView”的自定义视图是如何工作的,但如果它像我想的那样工作(从 imageView 扩展),它应该可以正常工作。如果没有,您可以简单地将图像文件下载到缓存,根据需要缩小它们,然后使用 imageViews 上的位图。如果它们已经是小图像(取决于服务器),您可以使用 Google 的 Volley 的“NetworkImageView”类。您还可以进行测试以查看您的自定义 ImageView 是否正常 - 只需使用 ImageView 并将其设置为 src 以指向您应用中的可绘制对象。
  • 我更新了上面的源代码和屏幕截图(缩略图仍然显得很小 - 现在偏离中心)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 2013-08-20
  • 2018-10-20
  • 2021-10-29
  • 2016-11-15
相关资源
最近更新 更多