android-不同屏幕上显示的界面不同
我是android编程新手,我的问题是我创建了一个简单的计算器界面,带有一些Lienarlayout,结果在设计显示屏和我的物理手机上不一样 我找不到任何解决办法。对于操作员,我将一些图像放在不同的可绘制文件夹(hdpi、xhdpi和xxhdpi)上 我将把我的xml文件放在这里android-不同屏幕上显示的界面不同,android,layout,Android,Layout,我是android编程新手,我的问题是我创建了一个简单的计算器界面,带有一些Lienarlayout,结果在设计显示屏和我的物理手机上不一样 我找不到任何解决办法。对于操作员,我将一些图像放在不同的可绘制文件夹(hdpi、xhdpi和xxhdpi)上 我将把我的xml文件放在这里 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.c
<?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:background="@android:color/background_light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="165dp"
android:background="@color/clrBlue"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="50dp"
android:text="567"
android:textColor="@android:color/background_light"
android:textSize="45sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="87dp"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="7"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="8"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="9"
android:textColor="@android:color/black"
android:textSize="30sp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
app:srcCompat="@drawable/divide" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="87dp"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="4"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="5"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="6"
android:textColor="@android:color/black"
android:textSize="30sp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
app:srcCompat="@drawable/multiply" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="87dp"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="1"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="2"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="3"
android:textColor="@android:color/black"
android:textSize="30sp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/background_light"
app:srcCompat="@drawable/subtract" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="192dp"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/button15"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="@color/clrBlue"
android:text="clear"
android:textSize="25sp" />
</LinearLayout>
<Button
android:id="@+id/button3"
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:text="0"
android:textColor="@android:color/black"
android:textSize="30sp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
app:srcCompat="@drawable/add" />
</LinearLayout>
</LinearLayout>
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/transparent"
android:paddingRight="50dp"
android:paddingTop="125dp"
app:srcCompat="@drawable/equal" />
</RelativeLayout>
由于尺寸不同,结果也不同。在布局显示的顶部,您将看到选择了Nexus 4,屏幕尺寸为4.7英寸,我认为Galaxy S7的尺寸为5.1英寸 此外,密度像素也可能存在差异。 您可以参考android文档了解不同的像素密度。
相对和线性布局不是创建计算器布局的最佳选择。要创建此布局,请尝试使用
GridLayout
或ConstraintLayout
。这将是一个更简单、更可靠的解决方案
已编辑
您想要实现的目标看起来像是TableLayout的一项任务。您认为问题在于在按钮和线性布局中使用静态尺寸。是的,我在按钮上使用了一些静态尺寸,但我认为在使用密度像素时,它可以适应所有屏幕尺寸。感谢您的答案!我已经看到了屏幕大小的不同,但是我们如何解决这个问题呢?谢谢你的回答!所以,为了使它适合所有屏幕,我必须避免线性布局?@Salimhalouche,很快,是的。LinearLayout是按垂直或水平顺序排列视图的绝佳工具,我现在不了解这两者。非常感谢我要试试网格布局