我正在使用以下代码将谷歌登录按钮放在我的应用程序中。但是按钮中的文本偏离中心。我怎样才能使它居中?
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:visibility="visible"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp" />
最佳答案
通过使用调试器和反编译的类代码进行检查,我注意到:
setStyle
中设置真正的按钮.它使用进程间绑定(bind) API 从其他库创建一些类,我无法识别该类,也无法找到其反编译的类代码。 因此,看起来像在真正的按钮上放置了一些手动填充,在带有 G 图标的一侧。他们为什么要这样做,不知道。从上面可以看出,在按钮从 XML 中膨胀后“修复”填充是非常安全的。我认为这在 RTL 的情况下也是安全的,因为图标一侧的填充总是更大,我认为(或者我当然希望如此,如果这在 RTL 中被破坏,请告诉我):
loginBinding!!.googButton.getChildAt(0)?.let {
val smaller = Math.min(it.paddingLeft, it.paddingRight)
it.setPadding(smaller, it.paddingTop, smaller, it.paddingBottom)
}
那当然是 Kotlin。
关于android - 如何在谷歌登录按钮android中居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41967615/