繁体   English   中英

如何根据屏幕分辨率调整布局和滚动视图的大小

[英]How can i resize the layout and scrollview according to screen resolution

我设置屏幕分辨率为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>

请帮助我通过程序设置布局,滚动视图和文本大小

首先,Android不支持“屏幕分辨率”的概念。 有屏幕尺寸和屏幕(或像素)密度。 由于您已经在使用密度无关的像素(dp),因此我想您的问题是关于支持不同的屏幕尺寸的。

支持其他屏幕尺寸的方法是拥有特定于尺寸的资源目录。 例如,除了res/layout (您现在可能已经有了上面的布局文件)之外,您还可以使用res/layout-large来支持大屏幕。 有关详细信息,请参见《指南》主题“ 支持多个屏幕”及其链接到的页面。

您可能还考虑不为视图指定确切的大小,而是让它们填充父级或包装其内容。 如果这对于您的布局不起作用,并且您需要指定视图大小,则可以将大小设置为变量,然后在res/valuesres/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>

这样,您只需要一种布局,并且可以通过随屏幕尺寸而变化的尺寸值进行参数化。

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" //would fill what ever size it wud be.
android:orientation="vertical" >
  <ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" //would fill what ever size it wud be.
    android:background="#86C3C6" >

暂无
暂无

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

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