【问题标题】:Different imageview size in cardview on different image screens不同图像屏幕上cardview中的不同imageview大小
【发布时间】:2018-10-13 20:58:44
【问题描述】:

我在cardview 中使用带有recycler 的imageview。我还为不同的屏幕尺寸创建了文件夹。它适用于小屏幕手机和平板电脑 但是超过 5.5 英寸的手机屏幕存在缩放问题

代码如下:

cardview.xml (420dp)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:card_view="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

     <android.support.v7.widget.CardView
         android:id="@+id/card_view"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_gravity="center"
         android:layout_margin="15dp"
         android:elevation="3dp"
         card_view:cardCornerRadius="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="@dimen/album_cover_height"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/ritu"/>


        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/thumbnail"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            android:textColor="#4c4c4c"
            android:textSize="15dp"
            android:textAlignment="center"/>


            </RelativeLayout>

          </android.support.v7.widget.CardView>

       </LinearLayout>

cardview.xml (600dp)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:card_view="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

     <android.support.v7.widget.CardView
         android:id="@+id/card_view"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_gravity="center"
         android:layout_margin="15dp"
         android:elevation="3dp"
         card_view:cardCornerRadius="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="@dimen/album_cover_height"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/ritu"/>
        <!---->

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/thumbnail"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            android:textColor="#4c4c4c"
            android:textSize="15dp"
            android:textAlignment="center"/>


            </RelativeLayout>

          </android.support.v7.widget.CardView>

       </LinearLayout>

所以在第二个屏幕截图中,您可以看到图像 scaleType = fitXY 不起作用。它适用于所有不同的屏幕,包括平板电脑以及使用相同的代码

【问题讨论】:

  • 你应该为不同的屏幕设计
  • 我设计了但没有得到准确的屏幕尺寸。您可以检查代码,它适用于 2 个屏幕。我设计了 4 种不同的屏幕 Default,360dp,420dp,560dp,720dp。

标签: android imageview android-cardview


【解决方案1】:

每个 Drawable 都作为单独的文件存储在 res/drawable 文件夹之一中。您应该将不同分辨率的图像存储在 res/drawable-mdpi, -hdpi, -xhdpi, -xxhdpi 子文件夹中。

您应该将每种类型的资源放在 project's res/ directory 的特定子目录中。请确保您是否正确执行了此操作。

您可以在清单部分中添加以下内容

<supports-screens
        android:resizeable="true"
        android:smallScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:normalScreens="true"
        android:anyDensity="true"/>

阅读Support different screen sizes

【讨论】:

  • 感谢帮助。我正在从服务器下载所有图像,那么如何将它们放在可绘制文件夹中。请进一步帮助我。
  • @PranshuK 如果你使用 Constrantlayout 会更好。 developer.android.com/training/constraint-layout
  • @PranshuK I'm downloading all images from the server, then how can I put them in drawable folder 如果您的图像来自服务器端,则不要存储它。
  • @PranshuK 在图像视图中添加以下属性android:adjustViewBounds="true"android:scaleType="fitXY"
  • @IntelliJAmiya:仅供参考,您(和我)参与的评论线程正在元站点中讨论:Can we stop the rude 'Pal' and 'Bro' comments
【解决方案2】:

1) 添加 机器人:adjustViewBounds =“真” 在图像视图标签内。

2) 制作相对布局 l、width 和 height 来包裹内容。

3) (可选)将比例类型设为 centercrop。

【讨论】:

  • 感谢您的帮助!但是对cardview.xml(600dp)不起作用我应该更改屏幕尺寸吗?
  • adjustViewbounds 可以帮助您在任何设备上获得正确的图像分辨率,即使您不会为每个设备创建单独的可绘制文件夹。如果你已经得到了预期的结果,你可以跳过这个。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 2018-03-22
  • 2013-03-30
  • 2016-09-10
  • 1970-01-01
  • 2021-04-18
相关资源
最近更新 更多