如何创建支持所有" normal"的各种应用程序? Android中的屏幕尺寸?

时间:2014-08-15 18:08:06

标签: android android-layout android-ui

正常屏幕尺寸从3.2英寸屏幕到5英寸屏幕不等。 如何创建仅支持各种正常屏幕尺寸的应用程序?

考虑一下您正在创建计算器应用程序,其中包含5个按钮行和5个按钮列,您提供的按钮大小为50dp(宽度)和50dp(高度),然后在大屏幕上看起来非常好但在3.2英寸屏幕上非常大如果在小屏幕上有多个按钮,则按钮会离开屏幕。这是非常有问题的。真是太令人沮丧。

关于我该如何做的任何建议? 我发现官方开发者指南几乎没用,因为他们没有使用示例正确解释。

我知道我们可以使用

支持多种屏幕尺寸
res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml

但即使在正常的屏幕尺寸下也存在太多变化。

这里是我的应用程序的示例代码,并考虑所有宽度和高度为50.请尝试下面的代码,并尝试在4英寸屏幕,3.2英寸屏幕,4.5英寸屏幕上运行它。如果您使用的是android studio,则可以轻松预览所有屏幕尺寸。

抱歉我的英语不好,我是android UI编程的新手,虽然我已经在android中创建了基本的应用程序,但从未做过适当的Ui编程。

<Button
        android:layout_width="@dimen/layout_normal_programmer_top_button_width"
        android:layout_height="@dimen/layout_normal_programmer_top_button_height"
        android:text="Binary"
        android:id="@+id/binary"
        android:layout_alignBottom="@+id/decimal"
        android:layout_toRightOf="@+id/decimal"
        android:layout_toEndOf="@+id/decimal"

        android:textSize="18sp"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_top_button_width"
        android:layout_height="@dimen/layout_normal_programmer_top_button_height"
        android:text="Octal"
        android:id="@+id/octal"
        android:layout_alignBottom="@+id/binary"
        android:layout_toRightOf="@+id/binary"
        android:layout_toEndOf="@+id/binary"

        android:textSize="18sp"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_top_button_width"
        android:layout_height="@dimen/layout_normal_programmer_top_button_height"
        android:text="Hex"
        android:id="@+id/hex"
        android:layout_toEndOf="@+id/octal"

        android:textSize="18sp"
        android:layout_alignTop="@+id/octal"
        android:layout_toRightOf="@+id/octal"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"
        android:layout_alignRight="@+id/clr"
        android:layout_alignEnd="@+id/clr" />

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="D"
        android:id="@+id/d"
        android:layout_below="@+id/decimal"
        android:layout_alignLeft="@+id/decimal"
        android:layout_alignStart="@+id/decimal"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white" />

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="E"
        android:id="@+id/abs"
        android:layout_toEndOf="@+id/d"
        android:layout_alignBottom="@+id/d"
        android:layout_toRightOf="@+id/d"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="F"
        android:id="@+id/f"
        android:layout_alignBottom="@+id/abs"
        android:layout_toRightOf="@+id/abs"
        android:layout_toEndOf="@+id/abs"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="&lt;"
        android:id="@+id/del"
        android:layout_alignBottom="@+id/f"
        android:layout_toRightOf="@+id/f"
        android:layout_toEndOf="@+id/f"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="C"
        android:id="@+id/clr"
        android:layout_alignBottom="@+id/del"
        android:layout_toRightOf="@+id/del"
        android:layout_toEndOf="@+id/del"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"
        android:layout_alignRight="@+id/and"
        android:layout_alignEnd="@+id/and" />

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="A"
        android:id="@+id/a"
        android:layout_below="@+id/d"
        android:layout_alignLeft="@+id/d"
        android:layout_alignStart="@+id/d"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="B"
        android:id="@+id/b"
        android:layout_alignBottom="@+id/a"
        android:layout_alignLeft="@+id/abs"
        android:layout_alignStart="@+id/abs"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="C"
        android:id="@+id/c"
        android:layout_alignBottom="@+id/b"
        android:layout_toRightOf="@+id/b"
        android:layout_toEndOf="@+id/b"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="OR"
        android:id="@+id/or"
        android:layout_alignBottom="@+id/c"
        android:layout_toRightOf="@+id/c"
        android:layout_toEndOf="@+id/c"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"/>

    <Button
        android:layout_width="@dimen/layout_normal_programmer_bottom_button_width"
        android:layout_height="@dimen/layout_normal_programmer_bottom_button_height"
        android:text="AND"
        android:id="@+id/and"
        android:layout_alignBottom="@+id/or"
        android:layout_toRightOf="@+id/or"
        android:layout_toEndOf="@+id/or"
        android:background="@drawable/programmer_button_pressed"
        android:textColor="@android:color/white"
        android:layout_alignRight="@+id/div"
        android:layout_alignEnd="@+id/div" />

1 个答案:

答案 0 :(得分:0)

避免使用设置的宽度和高度,并且不要使用太多控件使屏幕变得杂乱,它只会使您的应用程序难以让用户无论如何都要导航。包装内容并使用布局来操纵布局中控件的位置。所以而不是:

<Button
    android:layout_width="@dimen/layout_normal_programmer_top_button_width">
    <!--etc-->
</Button>

请改用以下内容:

<Button
    android:layout_width=wrap_content>
    <!--etc-->
</Button>