在Android中自定义Google SignInButton

时间:2017-05-16 11:15:27

标签: java android xml google-plus google-signin

我想在Android中自定义Google SignIn Button。目前,通过使用以下代码,我有一个非常基本的默认布局。

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

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

谢谢。

ps:我是Android新手

5 个答案:

答案 0 :(得分: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" />

您可以自定义按钮,但必须确保遵循guidelines。 编辑:查看此库custom signin button

如果您想自己动手,可以创建线性布局并添加图像。

答案 1 :(得分:1)

基本上,Google SignInButton是一个FrameLayout,您可以对其进行自定义 根据需要,但需要一些动态调整。

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. 在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
                 }
              }
           } 
    

    快乐编码

see the results

答案 2 :(得分:0)

我认为你必须务实地做,而不是像这样的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

答案 3 :(得分:0)

不需要任何图书馆。好的方法是创建完全custom layout然后将google btn放置在此布局中,并为width设置heightGoogle Sign in Button匹配父级,然后为Google设置Visibility 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登录btn将位于所有布局之上,您的自定义布局将在谷歌btn点击上执行触摸涟漪。

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> 

答案 4 :(得分:0)

  

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

如果可以使用小型4KB库,则可以简单地使用以下属性来获得所需的内容:

  • android:text="{string}" :要在按钮上设置文本。

  • app:isDarkTheme="{Boolean}" :在按钮的蓝色主题和白色主题之间切换。该库处理文本颜色和背景颜色的更改。它还可以处理按下按钮或单击按钮时的颜色变化。

步骤1 :将以下内容添加到您的应用模块级别build.gradle文件中:

dependencies {
    compile 'com.shobhitpuri.custombuttons:google-signin:1.0.0'
}

第2步:在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>

您可以在the answer问题中浏览Can I edit the text of sign in button on Google?以获得更多详细信息,或访问http://go.shobhitpuri.com/googlesignin博客。

希望这会有所帮助。

免责声明:我是该库的作者,并将此修复程序放入了一个库中,以便可以轻松地在不同项目中重复使用它。