【发布时间】:2020-04-26 22:27:32
【问题描述】:
阅读本文 (http://developer.android.com/guide/practices/screens_support.html) 后,我使用 xml 文件中的 dp 单元开发了一个完整的应用程序。但是,当我在不同的屏幕上测试应用程序时,布局要么太大要么太小。
我认为 dp 单元会为我解决这个问题。为什么没有呢?我不想使用 weight 属性,因为一切都已经完成了。
一个xml布局:
<ImageView
android:layout_width="match_parent"
android:layout_height="140dp"
android:src="@drawable/logo3"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/select_level"
android:textColor="#4cb122"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="20dp"
android:layout_marginTop="20dp"
/>
<Button
android:background="@drawable/red_button"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:text="@string/easy"
android:textSize="15dp"
android:onClick="playEasy"
style="custom_button"
/>
<Button
android:background="@drawable/green_button"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="@string/medium"
android:textSize="15dp"
android:onClick="playMedium"
style="custom_button"
/>
<Button
android:background="@drawable/blue_button"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:text="@string/unbeatable"
android:onClick="playUnbeatable"
style="custom_button"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="113dp"
android:textSize="15dp"
android:textColor="@color/secondTextColor"
android:text="@string/developed_by"
/>
我能做什么?谢谢!
【问题讨论】:
-
请提供minimal, complete, and verifiable example 证明您的问题。在这种情况下,它将包括布局文件、维度资源文件(如果您正在使用这些文件)和显示结果的屏幕截图。
-
1.您应该提供(其中一个)有问题的布局,也许还应该提供您的问题的屏幕截图。 2.
dp用于确保不同屏幕上的尺寸相同,它不会“缩放”布局本身。 -
它确保相同的大小?所以这不是我需要的!如何编写将自动缩放的代码?
-
我只使用一张图片,所以我认为我不需要将多张图片作为可绘制资源。如何缩放简单的 TextView 和 Button?
-
您应该使用 'sp' 而不是 'dp' 作为文本大小。阅读 - stackoverflow.com/questions/11638691/…