Android 如何在谷歌登录按钮中居中显示文本

Android 如何在谷歌登录按钮中居中显示文本,android,android-layout,google-signin,text-alignment,Android,Android Layout,Google Signin,Text Alignment,我正在使用以下代码将google登录按钮放入我的应用程序中。但是,按钮中的文本偏离中心。我怎样才能使它居中 <com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:lay

我正在使用以下代码将google登录按钮放入我的应用程序中。但是,按钮中的文本偏离中心。我怎样才能使它居中

<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" />


最好自己创建一个按钮。 我总是喜欢这种方法,因为我可以开发任何类型的谷歌登录按钮,因为我想

<RelativeView
 android:height="wrap_content"
 android:width="match_parent"
 android:marginLeft="20dp"
 android:marginRight="20dp">
      <ImageButton
         android:parentRight="true"
         android:id="google_icon"
         android:background="@null"
         android:padding="10dp"/>
      <TextView
         android:id="google_text"
         android:height="wrap_content"
         android:width="match_parent"
         android:centerHorizontal="true"
         android:text="Google Login"
         android:padding="10dp"/>
</RelativeView>


上面是代码的结构,您可以使用它来实现您想要的功能。

只需使用您自己的自定义视图按钮,而不是使用它们的内置按钮。但是你需要遵循指导方针。即使是facebook按钮,我也会根据自己的需要定制

<RelativeLayout
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="@drawable/border_radius_white"
    android:clickable="true"
    android:id="@+id/google_signin" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/g_logo"
        android:adjustViewBounds="true"
        android:layout_gravity="center_vertical"
        android:padding="12dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:typeface="normal"
        android:textSize="@dimen/log_button_size"
        android:text="@string/text_sign_in_with_google"
        android:textStyle="bold"
        android:textColor="@color/scGrey"/>

</RelativeLayout>

使用
重力
固定文本中心:

<com.google.android.gms.common.SignInButton
  ......
  android:gravity="center_vertical|center"
  ...... />

通过使用调试器和反编译的类代码进行检查,我注意到:

  • SignInButton类在
    setStyle
    中设置实按钮。它使用进程间绑定API从其他库中创建一些类,我无法识别该类,也找不到其反编译的类代码
  • 内部按钮设置了侧垫,在带有G图标的一侧,侧垫更大
  • G图标通过后台可绘制文件显示,而不是TextView可绘制文件开始
因此,似乎在实际按钮上添加了一些手动填充,位于G图标的一侧。不知道他们为什么要这么做。综上所述,在按钮从XML中膨胀后“修复”填充是非常安全的。我认为这在RTL的情况下也是安全的,因为带有图标的一侧的填充物总是更大,我认为(或者我确实希望如此,如果RTL中出现这种情况,请有人告诉我):


当然是科特林。

我解决这个问题的一个简单方法是在我的文本后添加空格。。所以不是

android:text=“与谷歌连接”

我把它改成了

android:text=“与谷歌连接”

将下划线替换为空格


然而,这是使用自定义按钮“com.shobhitpuri.custombuttons.GoogleSignInButton”

让您的文本通过@AdityaVyas Lakhan的建议“登录谷歌”不会改变文本的中心,只会使其变长。目前这没有任何作用<代码>登录按钮继承自FrameLayout。可能是以前的更改?
登录按钮
框架布局
,无法识别
android:gravity
    loginBinding!!.googButton.getChildAt(0)?.let {
        val smaller = Math.min(it.paddingLeft, it.paddingRight)
        it.setPadding(smaller, it.paddingTop, smaller, it.paddingBottom)
    }