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.