我可以编辑Google上的登录按钮文字吗?

Mar*_*oli 32 android google-plus google-play-services google-signin

我正在将我的应用程序与google plus集成.我已经安装了Google Play服务并登录了我的帐户.我也可以发布并加一个我想要的东西.

我的问题

我无法更改登录按钮的文本.

我的代码

<com.google.android.gms.common.SignInButton
        android:id="@+id/share_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Share on Google+" />
Run Code Online (Sandbox Code Playgroud)

我试过了什么?

任何帮助,将不胜感激.

编辑

如果不可能,有什么办法可以让我在另一个按钮上使用相同的谷歌登录按钮吗?

w.d*_*hue 98

这是我使用的技术:

protected void setGooglePlusButtonText(SignInButton signInButton, String buttonText) {
    // Find the TextView that is inside of the SignInButton and set its text
    for (int i = 0; i < signInButton.getChildCount(); i++) {
        View v = signInButton.getChildAt(i);

        if (v instanceof TextView) {
            TextView tv = (TextView) v;
            tv.setText(buttonText);
            return;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 同样存在风险,如果谷歌按钮更改实施,我们知道从谷歌服务和这个库涉及的每个更新. (6认同)
  • 我不知道为什么这没有被标记为肯定有效的正确答案.我很惊讶谷歌没有直接设置这个的方法 (4认同)
  • 这里大多数开发人员的狭隘视野给我留下了深刻的印象,建议一个人这样做是不好的,接受它作为答案更糟糕,甚至抱怨这不被接受,因为答案只表明人们还没有创建任何应用程序持续超过一年的 (2认同)

San*_*era 27

这是我使用的最简单的方法:

    TextView textView = (TextView) signInButton.getChildAt(0);
    textView.setText("your_text_xyz");
Run Code Online (Sandbox Code Playgroud)


Sho*_*uri 15

问题:

其他答案提到了一种解决方法.按钮的底层实现可能会随时更改,从而导致代码中断.尝试使用黑客行为让我觉得不舒服.对于一个干净的解决方案,你会认为设置android:textcom.google.android.gms.common.SignInButton你的布局文件会做的伎俩.但事实证明该属性不可用SignInButton.

目标

谷歌的指导方针

从文档中,Google建议创建自定义登录按钮上提到的自定义按钮.然后,它建议使用登录品牌指南中提到的品牌指南.这包括在按钮中使用给定的自定义图标和图像,设置特定的文本大小,填充以及徽标的其他做法和注意事项.

清理解决方案:

按照谷歌的建议做一些自定义工作.我愿意这样做,但想创造一些可重复使用的东西,以便其他人不必再次经历这一点.这就是我写一个快速小(4KB)库的原因.如果您发现问题,请随时为每个人的利益做出贡献.

  • 第1步:将以下内容添加到app模块级build.gradle文件中:

    dependencies {
        implementation 'com.shobhitpuri.custombuttons:google-signin:1.1.0'
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 第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>
    
    Run Code Online (Sandbox Code Playgroud)

用法

  • android:text="{string}":像往常一样设置按钮上的文本.

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

来源:

希望它可以帮助某人.