繁体   English   中英

Android Studio 设备大小不同

[英]Android Studio different sizes in devices

我是 android studio 的新手,我正在尝试使图像适合每台设备,但它在高密度设备中更小,我该如何解决这个问题。

这是我的物理设备

这是我的虚拟设备

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_60sdp"
        android:fontFamily="@font/glancesanslight"
        android:text="WELCOME"
        android:textAlignment="center"
        android:textColor="#00A083"
    
        android:textSize="@dimen/_40sdp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread" />
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_10sdp"
        android:fontFamily="@font/glancesansregular"
        android:letterSpacing=".15"
        android:text="TO CATLET"
        android:textAlignment="center"
        android:textColor="#007863"
    
        android:textSize="@dimen/_30sdp"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    

<ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_32sdp"
        android:layout_marginEnd="@dimen/_60sdp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:srcCompat="@drawable/mainartwork"
        tools:flow_horizontalAlign="start" />
    
    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_32sdp"
        android:background="@drawable/round_rect"
        android:fontFamily="@font/oswaldbold"
        android:paddingLeft="@dimen/_40sdp"
        android:paddingTop="@dimen/_2sdp"
        android:paddingRight="@dimen/_40sdp"
        android:paddingBottom="@dimen/_2sdp"
        android:text="BAŞLA"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="@dimen/_28sdp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

\</androidx.constraintlayout.widget.ConstraintLayout\>

我试过使用 sdp 和约束布局但没有开始工作。 我不太了解这些限制,就像我说的我完全是 android studio 的初学者,我有不同的 ldpi、mdpi ext 图像大小,但它看起来更小。

对于不同的屏幕尺寸,最好为不同的屏幕尺寸创建提供不同的布局设计。 也可以为小、中、高和超高密度屏幕分配不同的位图可绘制对象

res/layout/my_layout.xml             // normal screen size
res/layout-small/my_layout.xml       // small screen size
res/layout-large/my_layout.xml       // large screen size


res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density

Manifest 中的以下代码支持所有 dpis。

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

您的两个布局看起来基本相同,只是图形的大小给您带来了问题,对吧?

问题是您的图形定义的尺寸在测试设备(具有特定的分辨率和密度)上看起来正确,但在您的其他设备(具有不同的分辨率/密度)上看起来不正确。 当您将ImageViewwrap_content一起使用时,您允许其固有大小来定义视图在您的布局中应该有多大。

理想情况下,如果您使用ConstraintLayout ,您将使用约束来定义视图的大小,包括ImageView 然后,如果您明白我的意思,就可以使可绘制对象适合定义的区域,而不是相反!


有几种方法可以做到这一点 - 这是一个(我只是要添加重要的属性):

<ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="@dimen/_60sdp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"

        app:layout_constraintDimensionRatio="1.0"
        android:scaleType="fitCenter"

        app:srcCompat="@drawable/mainartwork"/>

所以这里发生了一些事情:

  • 宽度和高度设置为0dp ,因此它们完全由约束控制
  • 宽度由水平约束控制,水平约束将任一侧固定到父级的边缘,右侧的固定边距占用该宽度
  • 高度由constraintDimensionRatio属性控制,它是宽度的一个因素 - 在本例中为1.0 ,它与宽度匹配,所以视图是一个正方形
  • scaleType设置为fitCenter ,它缩放图像以适合视图,同时保持其纵横比

结果是您有一个正方形ImageView ,其尺寸由其宽度定义。 如果这很浪费(例如图像的宽度大于高度),那么您可以为图像使用适当的纵横比,而不是1.0 fitCenter确保无论视图的大小如何,图像都将适合其中而不会变形(如果您希望它拉伸以填充该区域,您可以使用fitXY )。


不幸的是,除了wrap_content (这会阻止您使用约束来控制布局)之外,我不确定是否有办法让它自动调整大小以适合图像。 并且有更多方法来定义布局,而不仅仅是固定 dp 边距(在不同宽度的设备上看起来会有所不同,尤其是在横向布局时)。

您可能要考虑的一件事是使用标题文本作为锚点:

问题中发布的布局

看到图形的边缘如何与文本的右边缘基本对齐了吗? 与其约束到父级的右边缘,不如约束到文本边缘。 这取决于你的布局是如何设计的! 图形是否应始终距离屏幕边缘60dp (或其他)? 还是应该始终与文本对齐? 在使用响应式设计时,您需要考虑当您拥有更多(或更少)空间时应该发生什么,事情应该放在哪里。 以及不同组件之间的关系是什么,从广义上讲,元素 X应该相对于元素 Y定位和调整大小,还是应该相对于屏幕可用空间?

在这方面有很多东西要学习,但请查看有关支持不同屏幕尺寸的文档以及有关响应式布局的部分。 也许这个ConstraintLayout 教程显示了一些您可用的工具,并且不要忘记您可以在 Layout Designer 预览窗口中使用不同的设备 - 检查您的设计在不同屏幕尺寸上的外观。 密度(如手机与平板电脑)等


只是对dpsp的快速概述。 dp是一种定义在任何设备上看起来基本相同的固定大小的方法。 对于每台设备, 160dp在屏幕上大约是一英寸。 所以当你定义一个60dp的边距时,这是一个大约1cm的固定大小。

在某些设备上,这是屏幕的一大块 - 在其他设备上,它只是屏幕的一小部分。 在手表上,这是它的一半,这就是为什么在构建响应式设计时需要牢记这种可变性(并且,对于手表示例,您可能需要完全不同的布局)

sp类似于dp ,但它根据用户的系统字体大小设置进行缩放。 所以这通常用于文本,因此他们可以控制它的大小以使其更舒适阅读 - 当您使用sp值时,您始终需要测试您的布局在不同大小设置下的外观,尤其是较大的设置!

但是您不必对所有文本使用sp 例如,您在图形上方的标题 - 这更像是一个设计元素,对吗? 你希望它有一定的大小,占用一定的空间。 它几乎就像一个标志。 而且——它已经很大了? 它是否需要扩大规模才能变得可读,我认为可能不需要 - 任何需要它的人都可能会遇到应用程序其余部分的问题。 因为文本的其余部分不太可能接近该大小。

因此,在这种情况下,我可能会使用dp ,这样您就可以在那里始终如一地布置您的设计(按钮上的sp很好 - 普通文本元素应始终使用sp以实现可访问性)。 我不确定那里的_60sdp dimen是多少( spdp ?)但最好使用正确的名称,这样就清楚了。


同样通常,您不会将dimen资源用于这样的显式值 - 如果它是60dp ,您也可以只写60dp 资源用于@dimen/horizontalMargin dimen/horizontalMargin 或@dimen/titleTextSize之类的东西 - 可能在元素之间共享的实际属性,或者可能根据屏幕尺寸等因素而有所不同。

通过在资源中定义实际值,您可以通过在一个地方更新值来更改使用该属性的所有内容。 拥有命名资源意味着更清楚为什么该文本具有特定大小,或者页边距具有特定宽度 - 您基本上是在描述它的目的。 定义一个名为@dimen/60dp的资源是两个世界中最糟糕的一个,它没有任何好处,只是做更多的工作!

(虽然如果你正在研究一个固定的网格想法,其中所有内容都是一组固定值中的一个,例如8dp16dp32dp等并且你想定义它们,我可以看到。但即便如此,它可能会更好以不同的方式命名它们,只是为了更清楚您在做什么以及为什么这样做。因为您是新手,所以请指出来!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM