简体   繁体   English

在Android中自定义Google SignInButton

[英]Customize Google SignInButton In Android

I want to customise Google SignIn Button In Android. 我想在Android中自定义Google SignIn Button。 Currently I have a very basic default layout by using following code. 目前,我使用以下代码有一个非常基本的默认布局。

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

I am not satisfied with the current button layout. 我对当前的按钮布局不满意。 Is it possible to update the button text, background colour in the default button, Or Should I create full custom button layout? 是否可以更新按钮文本,默认按钮中的背景颜色,或者我应该创建完整的自定义按钮布局吗?

Thank you. 谢谢。

ps: I am new to Android ps:我是Android新手

Its very simple , you need to do this 它很简单,你需要这样做

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

You can customize your button , but you will have to make sure you follow the guidelines . 您可以自定义按钮,但必须确保遵循指南 Edit: Check out this library custom signin button 编辑:查看此库自定义登录按钮

If you want to do it yourself you can create a linear layout and add images. 如果您想自己动手,可以创建线性布局并添加图像。

Basically, Google SignInButton is a FrameLayout you can customize it as you want but need some dynamic tweaks. 基本上,Google SignInButton是一个FrameLayout,您可以根据需要自定义它,但需要一些动态调整。

XML part adding custom background to the frame layout, make your own. XML部分向框架布局添加自定义背景,制作自己的。

  <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 在FrameLayout内部,我们有一个textView,因此您可以根据需要重新设计文本视图

      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 快乐的编码

see the results 看到结果

I think you have to do it pragmatically rather than xml something like this 我认为你必须务实地做,而不是像这样的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;
        }
    }
}

another way but not that secure 另一种方式但不安全

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

No need any library. 不需要任何图书馆。 The nice trick is to create your fully custom layout then place google btn in this layout and set width and height match parent for Google Sign in Button , then set Visibility for Google Btn = 0 and then in 不错的诀窍是创建完全custom layout然后在此布局中放置google btn ,并为Google Sign in Button设置widthheight匹配父级,然后为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);
            }
        });

So Google Sign In btn will be on top of all layouts and your custom layout will perform touch ripple on google btn click. 因此,Google登录btn将位于所有布局之上,您的自定义布局将在谷歌btn点击上执行触摸波动。

PS sure you have to implement touch ripple effect with any colors you want in your drawable. PS确定你必须用你想要的任何颜色实现触摸涟漪效果。 Example: 例:

<?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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM