[英]Setting up layout of calculator buttons on android
我剛剛開始學習Android。 首先,我只想創建一個與我們現有的Android手機類似的Calculator應用程序。 我使用了以下布局:
我想要第一個表布局的4行和第二個表布局的5行正確對齊。 我是否要刪除填充空間? 還是我需要手動設置按鈕的最小尺寸? 我設置的布局正確嗎?
`
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="112dp"
android:id="@+id/textView" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:text="7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button7" />
<Button
android:text="8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button8" />
<Button
android:text="9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button9"
android:elevation="0dp" />
</TableRow>
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button4" />
<Button
android:text="5"
android:id="@+id/button5"
android:layout_height="match_parent" />
<Button
android:text="6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button6" />
</TableRow>
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button1" />
<Button
android:text="2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button2" />
<Button
android:text="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button3" />
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="match_parent">
<Button
android:text="."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonDot"
android:minHeight="48dp" />
<Button
android:text="0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button0" />
<Button
android:text="="
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonequals"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:text="DEL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonDelete" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonDivide" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMultiply" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonPlus" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonSubtract" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
`
我建議您可以為該視圖使用GridLayout
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="4"
android:orientation="horizontal" >
</GridLayout>
您可以設置按鈕的高度,也可以添加填充。
順便說一句,我認為,高度為wrap_content
的父母和高度為match_parent
孩子是合法的,但有點奇怪。 我會給孩子們一個固定的高度,或者給wrap_content
加上足夠的填充,以獲得想要的外觀。
下面的代碼可以完成我想的工作,
<RelativeLayout 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"
xmlns:andriod="http://schemas.android.com/apk/res-auto"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="@+id/resultview"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="@+id/textView" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_below="@+id/resultview"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_alignParentBottom="true"
android:background="#000000">
<TableRow android:layout_width="match_parent"
android:minHeight="100dp"
android:orientation="vertical"
android:id="@+id/row1"
android:background="#000000">
<Button
android:text="7"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button7" />
<Button
android:text="8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:textColor="#ffffff"
android:id="@+id/button8" />
<Button
android:text="9"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button9"
android:elevation="0dp" />
</TableRow>
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/row2"
android:minHeight="100dp"
android:layout_below="@+id/row1">
<Button
android:text="4"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button4" />
<Button
android:text="5"
android:background="#000000"
android:textColor="#ffffff"
android:id="@+id/button5"
android:layout_height="match_parent" />
<Button
android:text="6"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button6" />
</TableRow>
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp">
<Button
android:text="1"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button1" />
<Button
android:text="2"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button2" />
<Button
android:text="3"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button3" />
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#000000">
<Button
android:text="."
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonDot"
android:minHeight="48dp" />
<Button
android:text="0"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button0" />
<Button
android:text="="
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonequals"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#000000">
<TableRow
android:layout_width="match_parent"
android:layout_height="10dp"
android:minHeight="80dp">
<Button
android:text="DEL"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonDelete" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="10dp"
android:minHeight="80dp">
<Button
android:text="/"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonDivide" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="10dp"
android:minHeight="80dp">
<Button
android:text="x"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonMultiply" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="10dp"
android:minHeight="60dp">
<Button
android:text="+"
android:background="#000000"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonPlus" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="10dp" >
<Button
android:text="-"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:textColor="#ffffff"
android:id="@+id/buttonSubtract" />
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>
轉到Custom ViewGroup和Custom Views作為子級。 您將擁有更大的靈活性,並且對性能的控制也將為您帶來好處。
順便說一句,創建自定義組件很容易。
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.