Android - 计算器支持所有屏幕尺寸
我是一个Android应用程序开发的初学者,我有一个问题,我找不到任何解决方案。我在Nexus 5个应用程序和它的作品完美,布局之间没有空的空间,但是当我切换到的Nexus 4,这是发生了什么(我用的颜色布局分开):Android - 计算器支持所有屏幕尺寸
如果我切换到像素XL:
我能做些什么,所以也没有对权利的任何空的空间(下面有以防万一贴你需要它的XML代码) 。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClickDTB"
android:text="Convert to Binary"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="14sp"
android:textStyle="bold|italic" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0f0">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="98dp"
android:layout_weight="0.01"
android:fontFamily="serif-monospace"
android:text="Calculator"
android:textAlignment="textEnd"
android:textColor="@android:color/white"
android:textSize="35dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#f0f"
android:orientation="horizontal">
<LinearLayout
android:layout_width="72.4dp"
android:layout_height="match_parent"
android:background="#FFA500"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="72.4dp"
android:layout_height="match_parent"
android:background="#000FFF"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="72.4dp"
android:layout_height="match_parent"
android:background="#8B0000"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="72.4dp"
android:layout_height="match_parent"
android:background="#800080"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="72.4dp"
android:layout_height="match_parent"
android:background="#2E8B57"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
必须使用以及重总和这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClickDTB"
android:text="Convert to Binary"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="14sp"
android:textStyle="bold|italic" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0f0">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="98dp"
android:layout_weight="0.01"
android:fontFamily="serif-monospace"
android:text="Calculator"
android:textAlignment="textEnd"
android:textColor="@android:color/white"
android:textSize="35dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f"
android:weightSum="100"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="20"
android:layout_height="match_parent"
android:background="#FFA500"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="20"
android:layout_height="match_parent"
android:background="#000FFF"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="#8B0000"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="20"
android:layout_height="match_parent"
android:background="#800080"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:background="#2E8B57"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
哦,我看到...相同的答案,这意味着它是做到这一点的方法 –
是的布局重量会将您的家长布局分为5个部分,完全覆盖您的屏幕,没有剩下右侧空间。 –
非常感谢你们,它实际上是在工作。 –
试试下面的代码
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClickDTB"
android:text="Convert to Binary"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="14sp"
android:textStyle="bold|italic" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0f0">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="98dp"
android:layout_weight="0.01"
android:fontFamily="serif-monospace"
android:text="Calculator"
android:textAlignment="textEnd"
android:textColor="@android:color/white"
android:textSize="35dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFA500"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000FFF"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#8B0000"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#800080"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#2E8B57"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
所以你实际添加layout_weight到所有的布局? –
是@ A.Tressos,如果需要适合所有屏幕尺寸,则需要添加。 –
https://developer.android.com/ guide/practices/screens_support.html https://developer.android.com/training/multiscreen/screensizes.html – Nenco
使用重量和重量总和属性为你布局 –
我见过每一个可用的链接,我试过了,但它似乎不工作。这就是为什么我问我需要某人来解释基于我的代码的问题,所以我明白一般会发生什么。 –