Android x Kotlin 實作課程:打造個人專屬計算機 APP EP3 佈局優化使用 Style 樣式包

更新於 發佈於 閱讀時間約 14 分鐘

打造個人專屬計算機 APP 系列文章目錄:
https://vocus.cc/article/65acea9ffd897800019c13dc

課程摘要

本課程學習如何提取共同屬性,透過 Style 樣式包,套用至每個按鈕。來提升佈局的可讀性和好維護性。

教學影片

若您無法順利觀看教學影片,請先登入您的 YouTube 帳號,然後點擊以下連結加入我們的頻道會員:

  • 一般會員:成為一般會員後,您將能夠觀看我們所提供的一般會員專屬線上課程。
  • 精實會員:成為精實會員後,您將能夠觀看我們所提供的精實會員專屬線上課程。

範例程式碼

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:layout_rowWeight">1</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">30sp</item>
</style>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:background="#050005">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="@+id/processTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:padding="8dp"
android:textColor="#efefef"
android:textColorHint="@color/white"
android:textSize="35sp"
tools:hint="計算機過程顯示區域"/>
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="end"
android:padding="8dp"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="45sp"
tools:hint="666"/>
</LinearLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:columnCount="4">

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonAllClear"
style="@style/MyButtonStyle"
android:text="AC" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonToggleSign"
style="@style/MyButtonStyle"
android:text="+/-" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonDivide"
style="@style/MyButtonStyle"
android:text="/"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonBackspace"
style="@style/MyButtonStyle"
android:text="⌫"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button7"
style="@style/MyButtonStyle"
android:text="7"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button8"
style="@style/MyButtonStyle"
android:text="8"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button9"
style="@style/MyButtonStyle"
android:text="9"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonMultiply"
style="@style/MyButtonStyle"
android:text="*"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button4"
style="@style/MyButtonStyle"
android:text="4"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button5"
style="@style/MyButtonStyle"
android:text="5"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button6"
style="@style/MyButtonStyle"
android:text="6"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonMinus"
style="@style/MyButtonStyle"
android:text="-"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button1"
style="@style/MyButtonStyle"
android:text="1"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button2"
style="@style/MyButtonStyle"
android:text="2"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button3"
style="@style/MyButtonStyle"
android:text="3"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonPlus"
style="@style/MyButtonStyle"
android:text="+"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="0"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textColor="@color/white"
android:textSize="30sp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonDot"
style="@style/MyButtonStyle"
android:text="."/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonEquals"
style="@style/MyButtonStyle"
android:text="="/>

</GridLayout>

</LinearLayout>
留言
avatar-img
留言分享你的想法!
avatar-img
HKT實驗室
24會員
261內容數
哈囉!歡迎光臨我的沙龍!我是 KT,一位對應用程式開發充滿熱情的開發者。在這個專屬空間,我將與您分享我在應用開發領域的深入學習心得和豐富的實戰經驗。如果您對應用程式開發技術同樣充滿好奇,渴望不斷探索新知,歡迎成為我們的會員,一起在應用程式開發的旅途上,探索更深層次的技術世界,享受學習的樂趣。
HKT實驗室的其他內容
2024/02/06
本課程學習如何透過函數重構程式碼,提高可讀性、可維護性和重用性。以按鈕操作為例,將不同邏輯提取成函數,例如 allClear()、equalSign() 等,使程式碼更簡潔易懂,提升開發效率。
Thumbnail
2024/02/06
本課程學習如何透過函數重構程式碼,提高可讀性、可維護性和重用性。以按鈕操作為例,將不同邏輯提取成函數,例如 allClear()、equalSign() 等,使程式碼更簡潔易懂,提升開發效率。
Thumbnail
2024/02/01
本課程學習如何實作處理加、減、乘和除按鈕等計算機相關邏輯。
Thumbnail
2024/02/01
本課程學習如何實作處理加、減、乘和除按鈕等計算機相關邏輯。
Thumbnail
2024/01/30
本課程學習如何處理按鈕點擊事件並透過 Logcat 日誌輸出資訊確認按鈕邏輯是否正確。
Thumbnail
2024/01/30
本課程學習如何處理按鈕點擊事件並透過 Logcat 日誌輸出資訊確認按鈕邏輯是否正確。
Thumbnail
看更多
你可能也想看
Thumbnail
每年4月、5月都是最多稅要繳的月份,當然大部份的人都是有機會繳到「綜合所得稅」,只是相當相當多人還不知道,原來繳給政府的稅!可以透過一些有活動的銀行信用卡或電子支付來繳,從繳費中賺一點點小確幸!就是賺個1%~2%大家也是很開心的,因為你們把沒回饋變成有回饋,就是用卡的最高境界 所得稅線上申報
Thumbnail
每年4月、5月都是最多稅要繳的月份,當然大部份的人都是有機會繳到「綜合所得稅」,只是相當相當多人還不知道,原來繳給政府的稅!可以透過一些有活動的銀行信用卡或電子支付來繳,從繳費中賺一點點小確幸!就是賺個1%~2%大家也是很開心的,因為你們把沒回饋變成有回饋,就是用卡的最高境界 所得稅線上申報
Thumbnail
全球科技產業的焦點,AKA 全村的希望 NVIDIA,於五月底正式發布了他們在今年 2025 第一季的財報 (輝達內部財務年度為 2026 Q1,實際日曆期間為今年二到四月),交出了打敗了市場預期的成績單。然而,在銷售持續高速成長的同時,川普政府加大對於中國的晶片管制......
Thumbnail
全球科技產業的焦點,AKA 全村的希望 NVIDIA,於五月底正式發布了他們在今年 2025 第一季的財報 (輝達內部財務年度為 2026 Q1,實際日曆期間為今年二到四月),交出了打敗了市場預期的成績單。然而,在銷售持續高速成長的同時,川普政府加大對於中國的晶片管制......
Thumbnail
重點摘要: 6 月繼續維持基準利率不變,強調維持高利率主因為關稅 點陣圖表現略為鷹派,收斂 2026、2027 年降息預期 SEP 連續 2 季下修 GDP、上修通膨預測值 --- 1.繼續維持利率不變,強調需要維持高利率是因為關稅: 聯準會 (Fed) 召開 6 月利率會議
Thumbnail
重點摘要: 6 月繼續維持基準利率不變,強調維持高利率主因為關稅 點陣圖表現略為鷹派,收斂 2026、2027 年降息預期 SEP 連續 2 季下修 GDP、上修通膨預測值 --- 1.繼續維持利率不變,強調需要維持高利率是因為關稅: 聯準會 (Fed) 召開 6 月利率會議
Thumbnail
本文總結和介紹了Kotlin的基本語法、註解和變數的使用,透過實例進行講解,以幫助讀者更好地理解和快速上手Kotlin語言。
Thumbnail
本文總結和介紹了Kotlin的基本語法、註解和變數的使用,透過實例進行講解,以幫助讀者更好地理解和快速上手Kotlin語言。
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
本課程學習如何透過函數重構程式碼,提高可讀性、可維護性和重用性。以按鈕操作為例,將不同邏輯提取成函數,例如 allClear()、equalSign() 等,使程式碼更簡潔易懂,提升開發效率。
Thumbnail
本課程學習如何透過函數重構程式碼,提高可讀性、可維護性和重用性。以按鈕操作為例,將不同邏輯提取成函數,例如 allClear()、equalSign() 等,使程式碼更簡潔易懂,提升開發效率。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News