【问题标题】:How to avoid change in position and size of UI element with the change of resolution in android?如何避免随着android中分辨率的变化而改变UI元素的位置和大小?
【发布时间】:2016-04-02 08:36:00
【问题描述】:

我正在开发我的第一个 android 应用程序并使用 xamarin。其他一切正常,但是当我在不同设备之间切换时,由于分辨率的变化,我的 UI 元素的位置和大小会发生变化。下面是我正在使用的 Main.xml 代码。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/avsar_ui"
        android:weightSum="100"
        android:minWidth="200px"
        android:minHeight="200px"
        android:id="@+id/linearLayout1">
        <EditText
            android:text="name"
            android:textColor="?android:attr/searchWidgetCorpusItemBackground"
            android:inputType="textPersonName"
            android:layout_marginTop="225.0dp"
            android:layout_marginBottom="25.0dp"
            android:layout_width="match_parent"
            android:background="@drawable/rounded_corner"
            android:layout_marginLeft="50.0dp"
            android:layout_marginRight="50.0dp"
            android:layout_height="wrap_content"
            android:id="@+id/PersonName" />
        <EditText
            android:text="Contact Number"
            android:textColor="?android:attr/searchWidgetCorpusItemBackground"
            android:inputType="numberSigned"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_corner"
            android:layout_marginLeft="50.0dp"
            android:layout_marginBottom="10.0dp"
            android:layout_marginRight="50.0dp"
            android:id="@+id/ContactNumber" />
        <EditText
            android:text="Address"
            android:textColor="?android:attr/searchWidgetCorpusItemBackground"
            android:inputType="textPostalAddress"
            android:layout_width="match_parent"
            android:background="@drawable/rounded_corner"
            android:layout_marginLeft="50.0dp"
            android:layout_marginRight="50.0dp"
            android:layout_marginBottom="10.0dp"
            android:layout_height="wrap_content"
            android:id="@+id/Address" />
        <Spinner
            android:layout_width="125.0dp"
            android:background="@drawable/dropbox_bg"
            android:layout_marginLeft="50.0dp"
            android:layout_height="40.0dp"
            android:id="@+id/Quantity" />
        <Button
           android:background="@drawable/order_button"
           android:layout_marginBottom="60dp"
           android:layout_marginTop="100dp"
           android:layout_marginLeft="115.0dp"
           android:layout_marginRight="115.0dp"
           android:layout_width="150.0dp"
           android:layout_height="50.0dp"
           android:id="@+id/OrderButton" />
    </LinearLayout>

【问题讨论】:

    标签: android xml user-interface resolution


    【解决方案1】:

    首先 - 避免使用 px,如果您想要可扩展的布局,请使用 dp。 其次 - 您可能正在不同的屏幕尺寸上测试您的布局。 Android 有 4 种屏幕尺寸:

    • xlarge 屏幕至少为 960dp x 720dp
    • 屏幕至少为 640dp x 480dp
    • 正常屏幕至少为 470dp x 320dp
    • 屏幕至少为 426dp x 320dp

    因此,虽然您的布局在不同的 普通 设备上看起来相同,但在 largexlarge 设备上会更小,因为它们的屏幕具有更多dps。您可能希望为每种屏幕尺寸声明单独的布局。

    有关更多信息,请参阅Android Developers 文档。

    【讨论】:

    • 将 px 更改为 dp dint help
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    相关资源
    最近更新 更多