Dạ xin chào mọi người! Em hiện tại đang xây dựng một ứng dụng máy tính trên Android. Vấn đề khó khăn của em là xây dựng bảng phím bấm. Em không biết phải dùng layout nào. Xin mọi người giúp đỡ ạ!
Tạo button trong ứng dụng máy tính
Bạn thử dùng table layout đi
Ở đây có một hướng dẫn chi tiết cách tạo button cho ứng dụng máy tính luôn này
http://mrbool.com/how-to-create-a-calculator-gui-for-android-using-xml/25745
Dạ e cảm ơn ạ.
E dùng lồng nhiều linear layout vào nhau
bạn share code với chụp hình giao diện lên đây xem nào
Nếu mà máy tính thì dùng table layout chắc hay hơn
không hay hơn đâu, t vừa thử xong
thiết kế dùng table layout xong, nhìn rất đẹp cho đến khi thử xoay ngang màn hình hoặc thay đổi kích thước màn hình
Đúng như khóa học mình đã dịch, chỉ nên dùng Frame Layout, Linear Layout và Relative Layout
Edit: Vẫn chưa thử lồng table layout vào trong linear layout :3
e lồng nhiều linear layout theo trang của ad @ltd share thì thấy ổn lắm ạ. Ngang dọc gì cũng không ảnh hưởng gì nhiều
Mấu chốt ở layout_weight ấy ạ. Lúc trước e ko biết cái này nên thử linear layout hoài ko được. Dưới là file xml của e ạ
<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/background_dark"
tools:context=".FullscreenActivity"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/extra"
android:layout_weight="3"
android:gravity="center|right"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/result"
android:layout_weight="1"
android:autoText="true"
android:textSize="40dp"
android:gravity="center|right"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="C"
android:id="@+id/delall"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:textSize="25dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="→"
android:id="@+id/next"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:textSize="40dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="DEL"
android:id="@+id/delete"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:textSize="25dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="÷"
android:id="@+id/devide"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:textSize="40dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="7"
android:id="@+id/seven"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="8"
android:id="@+id/eight"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="9"
android:id="@+id/nine"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="×"
android:id="@+id/multiply"
android:layout_weight="1"
android:textSize="40dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="4"
android:id="@+id/four"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="5"
android:id="@+id/five"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="6"
android:id="@+id/six"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="-"
android:id="@+id/minus"
android:layout_weight="1"
android:textSize="40dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="1"
android:id="@+id/one"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="2"
android:id="@+id/two"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="3"
android:id="@+id/three"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="+"
android:id="@+id/plus"
android:layout_weight="1"
android:textSize="40dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="0"
android:id="@+id/zero"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="π"
android:id="@+id/pi"
android:layout_weight="1"
android:textSize="25dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="."
android:id="@+id/dot"
android:layout_weight="1"
android:textSize="40dp" />
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="="
android:id="@+id/equal"
android:layout_weight="1"
android:textSize="40dp" />
</LinearLayout>
Thiết kế ổn đấy @HeikichiOguri Nhưng anh thấy có một vấn đề là cái số 0 bị ăn mất 1/4 khi xoay ngang. Em xem lại đừng dùng fix
layout_height
cho nó, thử dùng android:layout_height="wrap_content"
xem.
Mình có một vài góp ý hi vọng có ích:
- Nên dùng match_parent thay vì fill_parent: Cùng chức năng nhưng hai thuật toán khác nhau.
- Khi đặt weight của button nên để width = 0dp: Để tăng perform.
- android:textSize dùng đơn vị đo là sp không phải là dp. Về bản chất là giống nhau nhưng nếu để là dp thì android mất 1 công chuyển đổi qua đơn vị sp. SP là khoảng cách từ top bearing line đến bottom bearing line của text.
- Các khái niệm về đơn vị đo luôn luôn giống nhau nên cho vào dimen.
- Text nên cho vào string để dùng mutil language.
- Càng hạn chế dùng android:textAppearance càng tốt:Animate không phải lúc nào cũng tốt.
- Nên có thói quen ctrl + shifft + f