我将布局和 ScrollView 设置为屏幕分辨率 480x800。如何兼容其他屏幕分辨率? 这是我的代码。请帮助我。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:orientation="vertical" >
<ScrollView android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#86C3C6" >
<TextView
android:id="@+id/tvHistory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#86C3C6"
android:gravity="right"
android:focusable="true"
android:text=""
android:textColor="#FFFFFF" />
</ScrollView>
</RelativeLayout>
请帮我通过程序设置布局、 ScrollView 和文本大小
最佳答案
首先,Android不支持“屏幕分辨率”的概念。有屏幕尺寸和屏幕(或像素)密度。由于您已经在使用与密度无关的像素 (dp),我想您的问题是关于支持不同的屏幕尺寸。
支持其他屏幕尺寸的方法是拥有特定于尺寸的资源目录。例如,除了 res/layout
(您现在可能拥有上述布局文件)之外,您还可以使用 res/layout-large
来支持大屏幕。指南主题 Supporting Multiple Screens 中描述了有关此内容的详细信息。以及它链接到的页面。
您还可以考虑不指定 View 的确切大小,而是让它们填充父级或包裹其内容。如果这不适用于您的布局并且您需要指定 View 尺寸,则可以将尺寸设为变量并在 res/values
、res/values-large
等文件夹。例如,在 res/layout/main.xml
中,您可以拥有布局:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="@dimen/relative_layout_height"
android:orientation="vertical" >
<ScrollView android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="@dimen/scroll_view_height"
android:background="#86C3C6" >
<TextView
android:id="@+id/tvHistory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#86C3C6"
android:gravity="right"
android:focusable="true"
android:text=""
android:textColor="#FFFFFF" />
</ScrollView>
</RelativeLayout>
然后,在每个值文件夹中,您可以拥有一个 dimens.xml
文件,其中为相关尺寸指定了适当的值。在 res/values/dimens.xml
中(适用于中型屏幕):
<dimen name="relative_layout_height">150dp</dimen>
<dimen name="scroll_view_height">200dp</dimen>
在 res/values-large/dimens.xml
中,您将拥有:
<dimen name="relative_layout_height">250dp</dimen>
<dimen name="scroll_view_height">300dp</dimen>
这样,您只需要一种布局,并且它将通过随屏幕尺寸而变化的尺寸值进行参数化。
关于java - 如何根据屏幕分辨率调整布局和 ScrollView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9613271/