C# 安卓手机的布局与设计师的尺寸不同

C# 安卓手机的布局与设计师的尺寸不同,c#,android,layout,xamarin,C#,Android,Layout,Xamarin,目前,我正在开发一个显示一些值的移动应用程序(它们永远不会改变,所以会保存在列表中)。当我在designer中使用设备4.7 WXGA设计页面时,底部有很多空间,但在我的真实手机(Motorola Moto G3)上没有空间 现在我担心如果我在不同的手机(三星等)上部署应用程序会发生什么。我一直认为,使用类似AXML的东西创建UI的好处是让应用程序具有“响应性”,也就是说,它适合运行它的设备。但为什么这一切都没有发生 有人能给我一个提示如何归档这个吗 编辑: 这是我的AXML代码-在这里,我用文

目前,我正在开发一个显示一些值的移动应用程序(它们永远不会改变,所以会保存在列表中)。当我在designer中使用设备4.7 WXGA设计页面时,底部有很多空间,但在我的真实手机(Motorola Moto G3)上没有空间

现在我担心如果我在不同的手机(三星等)上部署应用程序会发生什么。我一直认为,使用类似AXML的东西创建UI的好处是让应用程序具有“响应性”,也就是说,它适合运行它的设备。但为什么这一切都没有发生

有人能给我一个提示如何归档这个吗

编辑: 这是我的AXML代码-在这里,我用文本替换了真实文本

<?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="#ffffff">
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:layout_marginTop="5dp">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2"
            android:layout_margin="5dp"
            android:gravity="center">
            <LinearLayout
                android:orientation="horizontal"
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout6">
                <ImageView
                    android:layout_width="32dp"
                    android:layout_height="40dp"
                    android:id="@+id/imgLeft"
                    android:src="@drawable/arrow_left"
                    android:layout_marginRight="50dp" />
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout5"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tbDin"
                android:textSize="35dp"
                android:layout_marginRight="15dp"
                android:textColor="@android:color/black" />
            <TextView
                android:layout_width="60dp"
                android:layout_height="match_parent"
                android:id="@+id/tbDinWert"
                android:textSize="35dp"
                android:textColor="@android:color/black"
                android:gravity="center"
                android:text="XX" />
            <ImageView
                android:layout_width="32dp"
                android:layout_height="40dp"
                android:id="@+id/imgRight"
                android:src="@drawable/arrow_right"
                android:layout_marginLeft="50dp" />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3"
        android:background="@android:color/transparent"
        android:layout_marginTop="5dp">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/linearLayout4"
            android:layout_margin="5dp"
            android:paddingLeft="15dp">
            <TextView
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/tb1"
                android:textSize="16dp"
                android:layout_marginRight="142dp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/TextView4"
                android:textSize="35dp"
                android:background="@android:color/transparent"
                android:inputType="none"
                android:textColor="@android:color/black"
                android:gravity="center" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/linearLayout4"
            android:layout_margin="5dp"
            android:paddingLeft="15dp">
            <TextView
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/tb2"
                android:textSize="16dp"
                android:layout_marginRight="142dp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/TextView4"
                android:textSize="35dp"
                android:background="@android:color/transparent"
                android:inputType="none"
                android:textColor="@android:color/black"
                android:gravity="center" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/linearLayout4"
            android:layout_margin="5dp"
            android:paddingLeft="15dp">
            <TextView
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/tb3"
                android:textSize="16dp"
                android:layout_marginRight="142dp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/TextView4"
                android:textSize="35dp"
                android:background="@android:color/transparent"
                android:inputType="none"
                android:textColor="@android:color/black"
                android:gravity="center" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/linearLayout4"
            android:layout_margin="5dp"
            android:paddingLeft="15dp">
            <TextView
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/tb3"
                android:textSize="16dp"
                android:layout_marginRight="142dp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/TextView4"
                android:textSize="35dp"
                android:background="@android:color/transparent"
                android:inputType="none"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/linearLayout4"
            android:layout_margin="5dp"
            android:paddingLeft="15dp">
            <TextView
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/tb4"
                android:textSize="16dp"
                android:layout_marginRight="142dp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/TextView4"
                android:textSize="35dp"
                android:background="@android:color/transparent"
                android:inputType="none"
                android:textColor="@android:color/black"
                android:gravity="center" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/linearLayout4"
            android:layout_margin="5dp"
            android:paddingLeft="15dp">
            <TextView
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/tb5"
                android:textSize="16dp"
                android:layout_marginRight="142dp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/TextView4"
                android:textSize="35dp"
                android:background="@android:color/transparent"
                android:textColor="@android:color/black"
                android:gravity="center" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/linearLayout4"
            android:layout_margin="5dp"
            android:paddingLeft="15dp">
            <TextView
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/tb6"
                android:textSize="10dp"
                android:layout_marginRight="142dp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/TextView4"
                android:textSize="35dp"
                android:background="@android:color/transparent"
                android:textColor="@android:color/black"
                android:gravity="center" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/linearLayout4"
            android:layout_margin="5dp"
            android:background="@android:color/transparent"
            android:paddingLeft="15dp">
            <TextView
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:id="@+id/tb7"
                android:textSize="10dp"
                android:textColor="@android:color/black"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="142dp"
                android:gravity="center" />
            <TextView
                android:text="Text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/TextView4"
                android:textSize="35dp"
                android:background="@android:color/transparent"
                android:textColor="@android:color/black"
                android:gravity="center" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


您必须记住,设备具有不同的屏幕大小和密度。不要在px中设置像素大小,而是在sp中设置字体(可缩放像素)。对于其他方面,使用dp而不是px,您必须通过创建布局ldpi、mdpi、hdpi、xhdpi来处理多个屏幕大小。因此,任何屏幕尺寸都存在,所有设备的外观和感觉都是相同的,字体也应该是“sp”和“dp”中的其他元素。

我认为,在应用程序中,您需要一个全尺寸的无拉伸图像。通过本教程,您可以在每个设备中调整图像,而不会出现问题

如果你想根据设备或操作系统的类型调整你的应用程序,也可以做其他事情

静态设备类包括几个属性和方法,这些属性和方法允许您的代码以非常简单和直接的方式在运行时处理设备差异:

  • Device.OS属性返回TargetPlatform的成员 枚举:iOS、Android、WinPhone或其他。WinPhone成员 指所有Windows和Windows Phone平台

  • Device.Idiom属性返回TargetIdiom的一个成员
    枚举:电话、平板电脑、桌面或不支持的

您可以在if和else语句或switch和case块中使用这两个属性来执行特定于特定平台的代码

在这本书中,你可以看到一个很好的例子,使用Device.OnPlatform第58页


我希望你能用这个解决你的问题。注意。

好的,首先您不能对像linearLayout4这样的视图使用相同的ID。第二,正确的做法是最好的做法,而不是创建这么多的布局,创建标题和列表视图,然后填充它。您是否有3项或333项使用ListView

接下来不要像这样使用android:layout_marginRight=“142dp”。边距和填充物应小得多,为避免拉伸物品的效果,请使用以下内容:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout4"
    android:layout_margin="5dp"
    android:gravity="center">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="16dp"
        android:textColor="@android:color/black"
        android:text="Value"
        android:layout_weight="0.5"
        android:gravity="center"/>
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="35dp"
        android:textColor="@android:color/black"
        android:layout_weight="0.5"
        android:gravity="center"/>
</LinearLayout>

您可以粘贴它以查看结果

尝试在你正在创建应用程序的程序中练习设计。如果您使用的是AndroidStudio,它具有预览功能,因此您可以检查不同大小的屏幕

对于任何布局或视图,如果您设置android:layout\u width=“match\u parent”android:layout\u height=“match\u parent”,它将始终适合设备


尝试创建具有不同背景的布局和视图,同时更改高度和宽度,以便可以看到正在发生的情况。

android:minWidth=“25px”,android:minHeight=“25像素。。。你的字体大小也在dp中,这些应该在SPD中。有很多android设备具有不同的屏幕大小和分辨率。如果您有上面这样的长内容,在某些设备中,该列表将跳出屏幕。所以最好把它放在一个滚动视图中。让我感到困扰的不仅仅是高度,还有宽度。在designer中,左侧和右侧都有足够的空间,但在我的手机上,文本靠近边缘。现在想象一部宽度较小的手机…想知道为什么将142dp设置为marginRight??我希望您尝试将1个文本视图与布局的左侧对齐,将1个文本视图与右侧对齐。要从左侧的不可见(背景#00000000-透明)文本视图调整文本文本视图,请在为每个分辨率阴极(ldpi、mdpi、hdpi和xhdpi)创建布局后进行调整我必须在代码中决定需要什么布局?根据屏幕分辨率,加载相应的布局。因此,在为每个分辨率阴极(ldpi、mdpi、hdpi和xhdpi)创建布局后,我必须在代码中决定需要什么布局?@Radinator,取决于屏幕分辨率,加载相应的布局在我看来,实际上不必定义所有这些不同的布局。我总是以一种适用于所有设备的方式使用wrap_content和match_parent,以及scrollview。除非你想在平板电脑上有一个好的布局,否则我会这样做。你能解释一下“创建标题和列表视图,然后填充它”的意思吗?创建一个布局,其中包含列名称和广告列表视图下面的名称。下面是一个你可以开始在android中尝试列表的例子。