custom google signin button
2.0.0
您想要為 Android 應用程式新增 Google 登入按鈕。但您想更改 Google 登入按鈕上的文字或提供自訂在地化嗎?您可能認為在佈局文件中的com.google.android.gms.common.SignInButton
上設定android:text
就可以了。但事實證明該屬性不適用於SignInButton
。
該程式庫可協助您使用標準android:text
屬性輕鬆地將文字新增至 Google 登入按鈕。它還允許您使用app:isDarkTheme="true"
屬性將按鈕主題設為深色或淺色。它遵循 Google 建立登入按鈕的指南。
淺色主題(白色) | 深色主題(藍色) |
---|---|
![]() | ![]() |
將以下內容新增至您的app
模組級build.gradle
檔案:
dependencies {
implementation ' com.github.shobhitpuri:custom-google-signin-button:2.0.0 '
}
另外,在頂級build.gradle
檔案中加入以下內容:
allprojects {
repositories {
google()
maven { url " https://jitpack.io " }
mavenCentral()
}
}
在您的 XML 佈局中,包含以下內容:
< RelativeLayout
...
xmlns : app = " http://schemas.android.com/apk/res-auto " >
< com .shobhitpuri.custombuttons.GoogleSignInButton
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : layout_centerInParent = " true "
android : text = " @string/google_sign_up "
app : isDarkTheme = " true " />
</ RelativeLayout >
app:isDarkTheme="{Boolean}"
:在按鈕的藍色主題和灰白色之間切換。該庫處理文字顏色和背景顏色的變更。它還可以處理按下按鈕或單擊按鈕時顏色的變化。android:text="{string}"
:照常設定按鈕上的文字。 在 Stackoverflow 上,顯然有人針對這個問題提出了問題。其中之一是我可以編輯 Google 上登入按鈕的文字嗎?他們中的許多人建議使用“hacks”,例如查找按鈕中的第一個TextView
或查找按鈕中的任何TextView
。他們的問題是,如果谷歌更新實施,他們可能會停止工作。
根據文檔,Google 建議建立一個自訂按鈕,如自訂登入按鈕中所述。然後,它建議使用登入品牌指南中提到的品牌指南。它包括在按鈕中使用自訂圖標和圖像、根據具體情況設定文字大小、填充以及徽標的其他注意事項。
按照 Google 的建議進行操作涉及一些自訂工作。這個 3.93 KB 的小型庫可以為您做到這一點。請隨時提出請求以改進庫。當我在實作登入按鈕時遇到這個問題時,我想建立一個可重複使用的解決方案。只是想與大家分享。
MIT License
Copyright (c) 2024 Shobhit Puri
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.