我正在尝试使用线性布局开发一个计算器应用程序,但是我不熟悉 android 开发,所以我不知道由谁来编写它......我如何制作它,例如一个中有 3 个按钮单行(它们占据了整个水平空间),然后第二行也包含 3 个按钮?
例子:
[ 7 8 9 ]
[ 4 5 6 ]
[ 1 2 3 ]
我正在尝试使用线性布局开发一个计算器应用程序,但是我不熟悉 android 开发,所以我不知道由谁来编写它......我如何制作它,例如一个中有 3 个按钮单行(它们占据了整个水平空间),然后第二行也包含 3 个按钮?
例子:
[ 7 8 9 ]
[ 4 5 6 ]
[ 1 2 3 ]
我建议使用TableLayout,因为在LinearLayout中您可能会遇到警告“嵌套权重对性能不利”。您需要在其中使用大量android:weightSum来填充所有空格。试试下面的那个。
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="3"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="3">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="7"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="8"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="9"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="3">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="4"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="5"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="6"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="3">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"
/>
</TableRow>
</TableLayout>
我建议使用 GridLayout。(http://developer.android.com/reference/android/widget/GridLayout.html)
由于 GridLayout 在 android 4 及更高版本上可用,您可能需要检查其他问题以添加旧版本的兼容性:
如果您想坚持使用 LinearLayout,您可能会想要这样的东西。正如其他人指出的那样, TableLayout 可能更健壮。我发现 LinearLayouts 非常简单。鉴于您的应用程序的简单性,我很难相信它会对任何 android 设备造成压力,除非正在屏幕上做动画和事情。另外,我确定您需要为计算器的其他组件留出空间,这应该很容易放入此布局中。您可以添加另一层嵌套或更改权重以容纳较小的行显示方程式/答案的顶部。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"
/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
/>
</LinearLayout>
</LinearLayout>
不要使用线性布局。请尝试在 RelativeLayout 中使用 RelativeLayouts。
您也可以使用 TableLayout,但我发现 RelativeLayout 可以更好地适应不同的屏幕几何形状。