Android - ImageView - 多屏幕支持 - 将 height/width 设置为 dp 或 match_parent/wrap_content?

Android - ImageView - Multiple Screen Support - Set height/width as dp or match_parent/wrap_content?

我目前正在尝试调整我的 Android 应用程序,以便它在多个 screens/devices.

上的外观和感觉相似

我现在知道其中的一个主要部分是根据 mdpi、hdpi、xhpi、xxhdpi 和 xxxhdpi 的 x1、x1.5、x2、x3、x4 比率为每个图像文件提供多种图像尺寸分别,我今天完成了。

完成此操作后,我在 @dimen.xml 值资源中定义了 Density independent Pixel 尺寸,与 MDPI 资源的实际图像尺寸(以像素为单位)相对应。随后,我将有问题的 layout_widthlayout_height 的图像视图设置为这个尺寸。

不过,我目前对为什么我的应用程序在 MDPI 仿真器上看起来与在 HDPI 仿真器上看起来仍然有很大不同一头雾水。为了强调我的意思,我将提供以下屏幕截图,其中显示了彼此相邻的 HDPI 和 MPDI 模拟器(左边是 HDPI (4" WVGA Nexus S),右边是 MDPI (5.4" FWVGA))。我希望它们看起来都像 MPDI 版本,但到目前为止我所做的显然不起作用。

我有自己的三个理论来解释为什么这没有按照我想要的方式工作:

1. 我不应该将 ImageView layout_widthlayout_height 设置为 dp 值,而是 match_parentwrap_content (?) (并在此过程中更改我的 .xml 布局的结构)。

2. 我不仅要定义多个可绘制资源,还要为不同的屏幕尺寸定义多个布局资源(?)。

3. 我误解了它应该如何工作的整个想法 (?)。

我还会给你一个列表项的例子,可以在第一个截图中看到(@drawable/phone_speed_icon 是 MPDI 中的 64 x64 像素资源和 96x96 HDPI 中的资源,并且 @dimen/icon_attribute_size64dp):

        <LinearLayout
            android:id="@+id/llSpeed_PreSession"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="10dp"
            android:paddingEnd="20dp"
            android:paddingStart="20dp"
            android:weightSum="100">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="@dimen/icon_attribute_size"
                android:layout_weight="20"
                android:weightSum="100">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="70"
                    android:gravity="center"
                    android:orientation="vertical">


                    <ImageView
                        android:layout_width="@dimen/icon_attribute_size"
                        android:layout_height="@dimen/icon_attribute_size"
                        android:src="@drawable/phone_speed_icon" />

                </LinearLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="30"
                    android:gravity="center_vertical"
                    android:paddingStart="10dp"
                    android:text="Speed"
                    android:textAppearance="@android:style/TextAppearance.Large"
                    android:textColor="#878787"
                    android:textStyle="bold" />

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="80"
                android:gravity="center">

                <android.support.v7.widget.SwitchCompat
                    android:id="@+id/swSpeed_PreSession"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent" />
            </LinearLayout>

        </LinearLayout>

最后,我想回答四个问题:

1. 如何使屏幕截图 1 中的列表项在 MDPI 设备上看起来与 HDPI 设备上的一样?这与我前面提到的三个理论之一有什么关系吗?

2. 为什么 header 文本 ("What do you want to measure?") 在一台设备上换行,而在另一台设备上没有换行?他们使用 sp 单位(通过 android:style/TextApperance.TextApperance.Large)?

3. HDPI 设备上的所有内容(如果有的话)难道不应该间隔更小而不是间隔更小吗? HDPI模拟器看起来好像"has got way less pixels available",如果你能理解我在说什么哪怕一点点。

4. 如何让第二张截图的Fragments看起来一样?或者我什至不想要这个,因为 HDPI(由于某种原因)物理上更小,这就是为什么布局不那么分散的原因?

无论如何,我一整天都在做这个,我读得越多,我就越困惑,所以非常感谢任何帮助!

您可以根据多种因素选择创建多个 dimens.xml 文件。在这里,您将看到我当前的项目在 Android Studio 中基于屏幕宽度的各种 dimens.xml 文件的外观。

但是,您可以对每个 dimens 文件有不同的要求。例如,您可以为每个密度创建 dimens 个文件: