Android 以像素定义大小
我有一个自定义布局的列表视图,它由左侧的一个图像和旁边的两个文本视图组成 由于列表项的高度不应高于图像,因此我考虑将高度定义为150px,即图像的大小 然而,这导致Android Studio向我发出警告 在这种情况下,以像素为单位设置项目的高度是否被视为不好的做法?如果是,我应该如何修复它Android 以像素定义大小,android,android-layout,Android,Android Layout,我有一个自定义布局的列表视图,它由左侧的一个图像和旁边的两个文本视图组成 由于列表项的高度不应高于图像,因此我考虑将高度定义为150px,即图像的大小 然而,这导致Android Studio向我发出警告 在这种情况下,以像素为单位设置项目的高度是否被视为不好的做法?如果是,我应该如何修复它 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge"
tools:text="Text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:textAppearanceMedium"
tools:text="Text" />
</LinearLayout>
这是我要创建的布局的图片:
以像素为单位设置项目高度是否被认为是错误的做法
这个案子
因为它将阻止android操作系统根据不同屏幕的可用空间来调整视图的大小
我该怎么修
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge"
tools:text="Text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:textAppearanceMedium"
tools:text="Text" />
</LinearLayout>
使用dp
代替px
px
平均屏幕上的实际像素数,因此与小密度屏幕相比,密度较高的设备每英寸的像素数将更多,因此在密度较高的屏幕上,视图将以较小的尺寸显示
dp
是显示独立像素,通过以下公式计算得出
px = dp * (dpi / 160)
一组六个广义密度:
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
以像素为单位设置项目高度是否被认为是错误的做法
这个案子
因为它将阻止android操作系统根据不同屏幕的可用空间来调整视图的大小
我该怎么修
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge"
tools:text="Text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:textAppearanceMedium"
tools:text="Text" />
</LinearLayout>
使用dp
代替px
px
平均屏幕上的实际像素数,因此与小密度屏幕相比,密度较高的设备每英寸的像素数将更多,因此在密度较高的屏幕上,视图将以较小的尺寸显示
dp
是显示独立像素,通过以下公式计算得出
px = dp * (dpi / 160)
一组六个广义密度:
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
您需要不同的图像大小才能显示 例如,mdpi为1,0x,hdpi为-1,5x,这意味着,如果图片的大小为mdpi 150px,则hdpi需要225px图片
而ListView的大小可能为150dp(mdpi为1,0x)。您需要不同的图像大小才能显示 例如,mdpi为1,0x,hdpi为-1,5x,这意味着,如果图片的大小为mdpi 150px,则hdpi需要225px图片
您的ListView的大小可能为150dp(mdpi为1,0x)。继续从注释进行讨论,您应该修改布局以动态处理其他视图。在版面中使用像素在99.999%的情况下是不好的做法(但请不要引用我的话继续评论中的讨论,您应该修改版面以与其他视图动态协作。在版面中使用像素在99.999%的情况下是不好的做法)(但请不要引用我的话,在Android中使用px在任何情况下都是不好的做法,tbh。你为什么不多说一些你试图实现的布局。也许是你想要的图片?然后我们可以帮助你“正确的”请看一下这个帖子:@BartekLipinski我已经编辑了我的问题,包括一个image@Larpee现在,您需要考虑当文本变大时(例如,由于设备文本缩放),它应该如何工作。看,它应该像
a
或b
那样工作吗?@BartekLipinski我选择了选项b)
,我编辑了我的问题,加入了我使用的代码。非常感谢你的帮助!在Android中使用px在任何情况下都是不好的做法。你为什么不多谈谈你想要达到的布局呢。也许是你想要的照片?然后,我们可以为您的案例提供“正确”的方法。看看这条线索:@BartekLipinski我已经编辑了我的问题,包括image@Larpee现在,您需要考虑当文本变大时(例如,由于设备文本缩放),它应该如何操作。看。它应该像a)
还是b)
那样工作?@BartekLipinski我选择了选项b)
,并编辑了我的问题,以包含我使用的代码。非常感谢你的帮助!我使用wordpress API获取图像,所以我不能包含不同大小的图像。我使用wordpress API获取图像,所以我不能包含不同大小的图像。这并不能解决问题。如果用户在其设备上使用文本缩放,则文本大小可能超过项目的150dp
高度。这将阻止向用户显示文本(或至少不会显示整个文本)@BartekLipinski用于文本缩放使用sp
而不是dp
使用sp
来缩放项目高度是一种不好的做法。@BartekLipinski哦,我认为它的作用是,我认为你想要缩放文本,所以我说sp
,但无论如何,如果是这种情况,那么我会使用它来解决问题。如果用户在其设备上使用文本缩放,则文本大小可能超过项目的150dp
高度。这将阻止向用户显示文本(或至少不会显示整个文本).@BartekLipinski用于文本缩放使用sp
而不是dp
使用sp
来缩放项目高度是一种糟糕的做法。@BartekLipinski哦,我认为它的作用是,我认为你想要缩放文本,所以我说sp
,但无论如何,如果是这样的情况,我会使用它