在安卓中自定义谷歌登录按钮

8

我希望自定义Android中的Google登录按钮。目前,我使用以下代码仅具有非常基本的默认布局。

<com.google.android.gms.common.SignInButton
            android:id="@+id/sign_in_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

我对当前的按钮布局不满意。是否可以更新默认按钮的按钮文本、背景颜色,或者应该创建完全自定义的按钮布局?

谢谢。

附注:我是 Android 新手。


1
您可以创建所需的自定义按钮,并在其上设置点击监听器,以便使用mSignInButton.performClick()执行单击事件。有关更多信息,请参阅此链接 - Jay
谷歌按钮不起作用,但Facebook按钮可以。对于谷歌有什么想法吗? - Nastromo
答案可能对于轻松自定义按钮文本和主题,并且是 Google 建议的方式非常有用。 - Shobhit Puri
4个回答

3

非常简单,您需要这样做

<Button
 android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:text="@string/common_signin_button_text_long" />

你可以自定义按钮,但是必须确保遵循指南。 编辑:请查看此库自定义登录按钮 如果你想自己做,可以创建一个线性布局并添加图片。

有什么办法可以在按钮左侧添加谷歌图标吗?我很苦恼。谢谢。 - user1590595
我更新了答案,如果你觉得我的回答有帮助,请接受并点赞。 - Dishonered

2
我认为你需要实用地做,而不是像xml这样的东西。
public static void customizeGooglePlusButton(SignInButton signInButton) {
    for (int i = 0; i < signInButton.getChildCount(); i++)
    {
        View v = signInButton.getChildAt(i);

        if (v instanceof TextView)
        {
            TextView tv = (TextView) v;
            tv.setText("My Text");
            tv.setAllCaps(true);
            tv.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
            //here you can customize what you want  
            return;
        }
    }
}

另一种方式,但不太安全

TextView textView = (TextView) signInButton.getChildAt(0);
//Customize here

1
基本上,Google SignInButton 是一个 FrameLayout,您可以自定义它,但需要进行一些动态调整。
XML 部分:向 FrameLayout 添加自定义背景,制作您自己的。
  <com.google.android.gms.common.SignInButton
                    android:id="@+id/google_signIn"
                    android:layout_width="match_parent"
                    android:background="@drawable/google_bottun_bg"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp">

                // google image logo
                <ImageView android:layout_width="16dp"
                           android:layout_height="16dp"
                           android:layout_gravity="center_vertical"
                           android:layout_marginLeft="12dp"
                           app:srcCompat="@drawable/ic_gmail"/>
  </com.google.android.gms.common.SignInButton>
  1. inside FrameLayout we have a textView so you can redesign text view as needed

      fun reDesignGoogleButton(signInButton: SignInButton, buttonText: String) {
             for (i in 0 until signInButton.childCount) {
              val v = signInButton.getChildAt(i)
              if (v is TextView) {
               v.text = buttonText //setup your text here
               v.setBackgroundResource(android.R.color.transparent) //setting transparent color that will hide google image and white background
               v.setTextColor(resources.getColor(R.color.white)) // text color here
               v.typeface = Typeface.DEFAULT_BOLD // even typeface 
               return
                 }
              }
           } 
    

    Happy Coding

查看结果


0
无需任何库。一个不错的技巧是创建自定义布局,然后在此布局中放置 google btn,并为 Google 登录按钮设置宽度和高度匹配父级,然后将 Google Btn 的可见性设置为 0,最后在...
googleSignInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                googleLayoutCustom.setPressed(true);
                Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                startActivityForResult(signInIntent, RC_SIGN_IN);
            }
        });

因此,Google登录按钮将位于所有布局的顶部,您的自定义布局将在Google按钮点击时执行触摸水波纹效果。

P.S 确保您要使用所需颜色在可绘制对象中实现触摸水波纹效果。例如:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorMain">

    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white" />

</ripple> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接