了解Android多设备布局

时间:2012-01-19 23:13:30

标签: android

好的,我已经阅读了有关多个设备布局和Android文档的所有其他帖子,但我还是设法填补了它!

我错误地使用我的HTC Sensation HD测试了这个应用程序。因此,当我决定在HTC Desire(中等屏幕分辨率)上模拟它时,想象一下我的震惊,它甚至不接近拟合。

我将DIP用于一切。那么,为什么在Desire中等分辨率下使用仿真器时屏幕的一半会脱落。我以为它只会缩小。如果没有,我该怎么做?

我附上了结果的图片。一个工作项目(一个愚蠢的版本)的Dropbox下载和附加在这里的xml。如您所见,四个控件已从页面中删除。

我需要此应用程序才能在大型,中型和小型设备上运行。 (Sensation HD,Desire,Cha Cha)。任何建议都会很棒。

PROJECT LINK

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dip"
android:paddingRight="20dip" >

<ImageView
    android:id="@+id/clientLogo_TextView"
    android:layout_width="wrap_content"
    android:layout_height="120dip"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal|center_vertical"
    android:layout_marginBottom="15dip"
    android:layout_marginTop="15dip"
    android:src="@drawable/logotop" 
    android:adjustViewBounds="true"/>

<Button
    android:id="@+id/buttonOne_Button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dip"
    android:text="Button One" >
</Button>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/customshape"
    android:orientation="vertical"
    android:padding="7dip" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/labelOne_TextView"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Label One"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/labelTwo_TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Label Two"
            android:textSize="13dip" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/buttonTwo_Button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:singleLine="true"
            android:text="Button Two"
            android:textSize="16dip" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/sliderLabel_TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:text="Slider"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/sliderStatus_TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" - Status"
            android:textSize="13dip" >
        </TextView>
    </LinearLayout>

    <SeekBar
        android:id="@+id/sliderOne_SeekView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="10" >
    </SeekBar>

    <TextView
        android:id="@+id/labelThree_TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Label Three"
        android:textStyle="bold" >
    </TextView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:layout_gravity="center_vertical|center_horizontal"
        android:orientation="horizontal" >

        <Spinner
            android:id="@+id/spinnerOne_Spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </Spinner>

        <Spinner
            android:id="@+id/spinnerTwo_Spinner"
            android:layout_width="wrap_content"
            android:layout_height="45dip"
            android:layout_weight="1" >
        </Spinner>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:orientation="horizontal"
        android:weightSum="1.0" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:orientation="vertical"
            android:weightSum="1.0" >

            <TextView
                android:id="@+id/labelFour_TextView"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight=".2"
                android:gravity="center_vertical|center_horizontal"
                android:text="Label Four"
                android:textStyle="bold" >
            </TextView>

            <Spinner
                android:id="@+id/spinnerThree_Spinner"
                android:layout_width="fill_parent"
                android:layout_height="45dip"
                android:layout_weight=".4" >
            </Spinner>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:orientation="vertical"
            android:weightSum="1.0" >

            <TextView
                android:id="@+id/labelFire_TextView"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight=".2"
                android:gravity="center_vertical|center_horizontal"
                android:text="Label Five"
                android:textStyle="bold" >
            </TextView>

            <Spinner
                android:id="@+id/spinnerFour_Spinner"
                android:layout_width="fill_parent"
                android:layout_height="45dip"
                android:layout_weight=".4" >
            </Spinner>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/ButtonsLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip" >

    <Button
        android:id="@+id/buttonThree_Button"
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:layout_margin="0dip"
        android:layout_weight="1"
        android:text="Button Three" >
    </Button>

    <Button
        android:id="@+id/buttonFour_Button"
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:layout_weight="1"
        android:text="Button Four" >
    </Button>
</LinearLayout>

1 个答案:

答案 0 :(得分:3)

与设备无关的像素(倾角)旨在使您的像素测量在不同的屏幕密度上保持一致。屏幕尺寸和密度是两个不同的东西。您仍然需要对布局进行编码以跨越物理屏幕尺寸。

例如,现在几乎所有10英寸平板电脑都有一个中等密度的屏幕,尽管它们具有“超大”的屏幕尺寸。这是因为即使屏幕体积很大,它也不像像Galaxy Nexus这样的高端手机一样密集像素。

为了使您的布局适应不同的屏幕尺寸,您需要在布局结构中考虑到这一点。这意味着几件事:

  • 尽可能避免使用硬编码尺寸。如果可以,请使用match_parent允许UI小部件包含可用空间。
  • 尽量不要叠加 许多控件,所以他们离开你最小的设备的屏幕 正在使用,或将控件放入ScrollView
  • 中心控件 可用的屏幕空间。您可以使用a对一组控件进行分组 布局容器,例如LinearLayout,然后将其置于其中 另一个android:layout_gravity的布局管理器。你也可以 在某些小部件上使用android:gravity(特别是TextViewImageView) 将他们的内容集中在视图本身的范围内。
  • Android允许您为每种屏幕尺寸和密度组合创建自定义布局,如果您需要为特定类别的设备(例如7英寸平板电脑或3英寸手机)定制布局。请参阅Android SDK docs
  • 要跨越各种各样的屏幕,您可以充分利用 Fragments API用于在平板电脑大小的屏幕上堆叠相关布局,但是 在电话屏幕上单独显示它们。

还有其他技术,但需要注意的一点是,使用dip不会自动缩放布局以包含所有屏幕尺寸。您必须解决屏幕尺寸和密度矩阵。

相关问题