代码之家  ›  专栏  ›  技术社区  ›  Muhammad Sarib

android studio中xml上的图像在高度上异常拉伸

  •  2
  • Muhammad Sarib  · 技术社区  · 6 年前

    编辑:我的图片的问题是分辨率太高,所以分辨率越高,空白空间就越多。但我无法得到正确的解决方案。如果分辨率太高,问题就在这里,如果它足以使图像适合720p显示,那么图像质量在qhd或更高的显示器上是如此糟糕。我似乎在网上找不到任何其他解决方案,除了解决问题的办法,所以请帮忙!

    我放在XML上的图像没有可见的边缘布局。我没有把这个属性放在那里,如果我写的话,它确实把边距垂直放置,导致了一个混乱的局面。即使我把所有内容都打包了,也会发生这种情况。这是我在代码中插入的图像:

    Image

    这就是我得到的结果:

    ![result

    这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/backgroundcolor"
        tools:context=".RegistrationActivity">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="10dp">
    
                    <androidx.cardview.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/android_course_thumbnail"
                            android:layout_marginBottom="20sp"/>
    
                    </androidx.cardview.widget.CardView>
    
                </LinearLayout>
    
            </ScrollView>
    
        </RelativeLayout>
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="?attr/backgroundcolor"
            app:headerLayout="@layout/navigation_header"
            app:menu="@menu/navigation_menu" />
    
    </androidx.drawerlayout.widget.DrawerLayout>
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Athira    6 年前

    删除ScrollView,并将图像大小调整为480x800分辨率

        2
  •  0
  •   suresh madaparthi    6 年前

    试试看

         <?xml version="1.0" encoding="utf-8"?>
          <android.support.constraint.ConstraintLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools">
    
       <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:fitsSystemWindows="true"
        android:layout_height="match_parent"
        tools:openDrawer="start">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
                        <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:layout_height="match_parent">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="10dp">
    
                    <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
    
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_gravity="center"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_launcher_background"
                            android:layout_marginBottom="20sp"/>
    
    
                    </android.support.v7.widget.CardView>
    
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="10dp">
    
                    <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
    
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_gravity="center"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_launcher_background"
                            android:layout_marginBottom="20sp"/>
    
    
                    </android.support.v7.widget.CardView>
    
                </LinearLayout>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:padding="10dp">
    
                        <android.support.v7.widget.CardView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
    
                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_gravity="center"
                                android:layout_height="wrap_content"
                                android:src="@drawable/ic_launcher_background"
                                android:layout_marginBottom="20sp"/>
    
    
                        </android.support.v7.widget.CardView>
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:padding="10dp">
    
                        <android.support.v7.widget.CardView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
    
                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_gravity="center"
                                android:layout_height="wrap_content"
                                android:src="@drawable/ic_launcher_background"
                                android:layout_marginBottom="20sp"/>
    
    
                        </android.support.v7.widget.CardView>
    
                    </LinearLayout>
    
                </LinearLayout>
            </ScrollView>
    
    
        </RelativeLayout>
    
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/colorAccent"
            app:headerLayout="@layout/listrow"
            app:menu="@menu/main" />
    
    
    </android.support.v4.widget.DrawerLayout>
    </android.support.constraint.ConstraintLayout>
    
        3
  •  0
  •   Muhammad Waqas    6 年前

    在ImageView的XML中使用scaleType=“fitxy”