C# 不同Android版本上的相同设计
我刚到Xamarin,昨天就开始工作了。我正试图在Visual Studio 2017中使用它制作一个Android应用程序,它在Android 8.0版上运行良好,但在v。7.0屏幕上的控制元件进入不同的位置 安卓8.0: 安卓7.0: 设计器代码:C# 不同Android版本上的相同设计,c#,android,visual-studio,xamarin,C#,Android,Visual Studio,Xamarin,我刚到Xamarin,昨天就开始工作了。我正试图在Visual Studio 2017中使用它制作一个Android应用程序,它在Android 8.0版上运行良好,但在v。7.0屏幕上的控制元件进入不同的位置 安卓8.0: 安卓7.0: 设计器代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Available Queries:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/queriescnt"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/queriescnt"
android:id="@+id/email"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="35dp"
android:layout_marginRight="326.0dp"
android:singleLine="true" />
<Button
android:text="Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/email"
android:id="@+id/submit"
android:layout_marginVertical="20dp"
android:layout_marginHorizontal="10dp" />
<EditText
android:layout_width="249.0dp"
android:layout_height="wrap_content"
android:layout_below="@id/submit"
android:id="@+id/keybox"
android:layout_marginVertical="300dp"
android:layout_marginHorizontal="50dp"
android:layout_marginRight="201.5dp"
android:password="true"
android:inputType="none|textPassword" />
<TextView
android:text="KEY:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/submit"
android:id="@+id/textView1"
android:layout_marginVertical="320dp"
android:layout_marginHorizontal="10dp" />
<CheckBox
android:text="S"
android:layout_width="54.5dp"
android:layout_height="wrap_content"
android:layout_below="@id/submit"
android:id="@+id/store"
android:layout_marginVertical="315dp"
android:layout_marginHorizontal="300dp" />
<TextView
android:text="Current line count:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/submit"
android:id="@+id/dbcount"
android:gravity="center_horizontal|center_vertical"
android:textSize="40dp"
android:inputType="none|textMultiLine" />
</RelativeLayout>
你能帮我让所有的元素在不同的版本上看起来一样吗?我真的很感激,如果你也建议我,我可以做得更好 您的页边距正在打乱您的布局。您不能只向视图添加更多边距,以相对于另一个视图进行定位 我已经调整了您的布局,以匹配您在第一个屏幕截图中的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/queriescnt"
android:text="Available Queries:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/queriescnt"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="35dp"
android:maxLines="1" />
<Button
android:id="@+id/submit"
android:text="Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/email"
android:layout_marginVertical="20dp"
android:layout_marginHorizontal="10dp" />
<TextView
android:id="@+id/dbcount"
android:text="Current line count:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/submit"
android:gravity="center_horizontal|center_vertical"
android:textSize="40dp"
android:inputType="none|textMultiLine" />
<TextView
android:id="@+id/textView1"
android:text="KEY:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<CheckBox
android:id="@+id/store"
android:text="S"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_margin="10dp"
android:layout_alignParentBottom="true" />
<EditText
android:id="@+id/keybox"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:password="true"
android:inputType="none|textPassword"
android:layout_alignBaseline="@+id/textView1"
android:layout_toEndOf="@+id/textView1"
android:layout_toStartOf="@+id/store"
tools:text="abc" />
</RelativeLayout>
您可能会注意到,我使用了toEndOf
,toStartOf
,alignBaseline
,alignParentStart
,alignParentEnd
和alignParentBottom
,而不是您使用的边距
只需在视图中添加300dp边距,就可以在您处理设计器预览的视图端口中工作。然而,并非所有屏幕在物理尺寸和像素密度方面都具有相同的宽度和高度。因此,300dp在所有屏幕上看起来都不一样。您的边距会弄乱您的布局。您不能只向视图添加更多边距,以相对于另一个视图进行定位 我已经调整了您的布局,以匹配您在第一个屏幕截图中的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/queriescnt"
android:text="Available Queries:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/queriescnt"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="35dp"
android:maxLines="1" />
<Button
android:id="@+id/submit"
android:text="Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/email"
android:layout_marginVertical="20dp"
android:layout_marginHorizontal="10dp" />
<TextView
android:id="@+id/dbcount"
android:text="Current line count:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/submit"
android:gravity="center_horizontal|center_vertical"
android:textSize="40dp"
android:inputType="none|textMultiLine" />
<TextView
android:id="@+id/textView1"
android:text="KEY:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<CheckBox
android:id="@+id/store"
android:text="S"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_margin="10dp"
android:layout_alignParentBottom="true" />
<EditText
android:id="@+id/keybox"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:password="true"
android:inputType="none|textPassword"
android:layout_alignBaseline="@+id/textView1"
android:layout_toEndOf="@+id/textView1"
android:layout_toStartOf="@+id/store"
tools:text="abc" />
</RelativeLayout>
您可能会注意到,我使用了toEndOf
,toStartOf
,alignBaseline
,alignParentStart
,alignParentEnd
和alignParentBottom
,而不是您使用的边距
只需在视图中添加300dp边距,就可以在您处理设计器预览的视图端口中工作。然而,并非所有屏幕在物理尺寸和像素密度方面都具有相同的宽度和高度。因此,300dp在所有屏幕上看起来都不一样。您可以尝试以下代码。我已经在安卓7.0和安卓8.0上测试了代码,它工作正常
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Available Queries:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/queriescnt"
android:textAlignment="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/queriescnt"
android:id="@+id/email"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="35dp"
android:layout_marginRight="326.0dp"
android:singleLine="true" />
<Button
android:text="Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/email"
android:id="@+id/submit"
android:layout_marginVertical="20dp"
android:layout_marginHorizontal="10dp" />
<TextView
android:text="Current line count:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/submit"
android:id="@+id/dbcount"
android:gravity="center_horizontal|center_vertical"
android:textSize="40dp"
android:inputType="none|textMultiLine" />
<LinearLayout
android:layout_marginTop="200dp"
android:gravity="center"
android:layout_marginBottom="12dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dbcount"
android:orientation="horizontal">
<TextView
android:text="KEY:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_marginHorizontal="10dp" />
<EditText
android:hint="input"
android:layout_width="249.0dp"
android:layout_height="wrap_content"
android:id="@+id/keybox"
android:layout_toRightOf="@id/textView1"
android:password="true"
android:inputType="none|textPassword" />
<CheckBox
android:text="S"
android:layout_width="54.5dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/keybox"
android:id="@+id/store"/>
</LinearLayout>
</RelativeLayout>
安卓7.0
安卓8.0
注:
布局\u
属性设置不正确。为以下控件添加父布局时,只需为父布局添加属性
android:layout_下方=“@id/dbcount”
您可以尝试以下代码。我已经在安卓7.0和安卓8.0上测试了代码,它工作正常
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Available Queries:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/queriescnt"
android:textAlignment="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/queriescnt"
android:id="@+id/email"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="35dp"
android:layout_marginRight="326.0dp"
android:singleLine="true" />
<Button
android:text="Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/email"
android:id="@+id/submit"
android:layout_marginVertical="20dp"
android:layout_marginHorizontal="10dp" />
<TextView
android:text="Current line count:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/submit"
android:id="@+id/dbcount"
android:gravity="center_horizontal|center_vertical"
android:textSize="40dp"
android:inputType="none|textMultiLine" />
<LinearLayout
android:layout_marginTop="200dp"
android:gravity="center"
android:layout_marginBottom="12dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dbcount"
android:orientation="horizontal">
<TextView
android:text="KEY:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_marginHorizontal="10dp" />
<EditText
android:hint="input"
android:layout_width="249.0dp"
android:layout_height="wrap_content"
android:id="@+id/keybox"
android:layout_toRightOf="@id/textView1"
android:password="true"
android:inputType="none|textPassword" />
<CheckBox
android:text="S"
android:layout_width="54.5dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/keybox"
android:id="@+id/store"/>
</LinearLayout>
</RelativeLayout>
安卓7.0
安卓8.0
注:
布局\u
属性设置不正确。为以下控件添加父布局时,只需为父布局添加属性
android:layout_下方=“@id/dbcount”
谢谢!我终于完成了我的应用程序,现在我所有的后续项目都将得到很好的设计:}非常感谢!我终于完成了我的应用程序,现在我所有的后续项目都将得到很好的设计:}