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

问题描述 投票:0回答:9

我正在将我的应用程序与 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+" />

我尝试过什么?

  • 首先,我尝试将此行添加到 xml

    android:text="Share on Google+"
    
  • 其次,我尝试以编程方式设置文本,但没有成功。

如有任何帮助,我们将不胜感激。

编辑

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

android google-play-services google-plus google-signin
9个回答
116
投票

这是我使用的技术:

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;
        }
    }
}

36
投票

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

    TextView textView = (TextView) signInButton.getChildAt(0);
    textView.setText("your_text_xyz");

19
投票

问题:

其他答案提到了解决方法。按钮的底层实现可能随时发生变化,这会导致代码中断。我在尝试使用这些技巧时感到不舒服。对于一个干净的解决方案,您可能会认为在布局文件中的

android:text
上设置
com.google.android.gms.common.SignInButton
就可以解决问题。然而事实证明该属性不适用于
SignInButton

瞄准

Google 指南

根据文档,Google 建议创建一个自定义按钮,如自定义登录按钮 [现已存档。请参阅存档页面此处]。它建议使用登录品牌指南中提到的品牌指南。这包括在按钮中使用给定的自定义图标和图像,设置特定的文本大小、填充以及徽标的其他注意事项。

清洁解决方案

按照 Google 的建议进行操作涉及一些自定义工作。我愿意这样做,但想创建一些可重复使用的东西,这样其他人就不必再经历这个了。这就是为什么我编写了一个快速的小型 (4KB) 库来做到这一点。如果您发现问题,请随时为每个人的利益做出贡献。

  • 第 1 步: 将以下内容添加到您的

    app
    模块级别
    build.gradle
    文件:

      dependencies {
          implementation 'com.github.shobhitpuri:custom-google-signin-button:2.0.0'
      }
    
  • 第 2 步: 另外,在顶级 build.gradle 文件中添加以下内容:

      allprojects {
          repositories {
              google()
              maven { url "https://jitpack.io" }
              mavenCentral()
          }
      }
    
  • 第 3 步: 在 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>
    

用法

  • android:text="{string}"
    :照常设置按钮上的文本。

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

来源

希望它对某人有帮助。


1
投票

android:text
不起作用,因为 Google 的登录按钮是
FrameLayout
,而不是
Button

由于文本属性仅适用于表示文本格式的视图,而不适用于 ViewGroups,因此您的解决方案不起作用。

实现的唯一方法是在

TextView
中定义
FrameLayout
,如 w.donahue 所解释。


1
投票

您可以使用我根据

w.donahue
的答案编写的课程,您可以在此页面中找到:

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.android.gms.common.SignInButton;

public class GoogleLoginButton extends FrameLayout implements View.OnClickListener{

    private SignInButton signInButton;
    private OnClickListener onClickListener;

    public GoogleLoginButton(Context context) {
        super(context);
        init();
    }

    public GoogleLoginButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GoogleLoginButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        signInButton = new SignInButton(getContext());
        signInButton.setSize(SignInButton.SIZE_STANDARD);
        setGooglePlusButtonText(signInButton, "Test");
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        addView(signInButton, params);
    }

    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;
            }
        }
    }

    @Override
    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        if(this.signInButton != null) {
            this.signInButton.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        if(this.onClickListener != null && v == this.signInButton) {
            this.onClickListener.onClick(this);
        }
    }
}

1
投票

对于初学者

避免一些崩溃。

try {
            ((TextView) mGoogleSignOutBtn.getChildAt(0)).setText(R.string.sign_out);
} catch (ClassCastException | NullPointerException e) {
            e.printStackTrace();
}

1
投票

这对我有用:

XML 文件

<com.google.android.gms.common.SignInButton
         android:id="@+id/google_login_button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                    />
                <Button
                    android:id="@+id/new_googlebtn"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:backgroundTint="@color/white"
                    android:text="@string/google_login"
                    android:textColor="@color/black"
                    app:icon="@drawable/googleg_standard_color_18"
                    app:iconGravity="textStart"
                    app:iconPadding="10dp"
                    app:iconTint="#00100D0D"
                    app:iconTintMode="src_atop" />

未修改的主要活动文件

google_signInButton=findViewById(R.id.google_login_button);
        google_signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
                    startActivityForResult(intent, SIGN_IN);

            }
        });

更新了主要活动文件

google_signInButton=findViewById(R.id.google_login_button);
        new_googlebtn=findViewById(R.id.new_googlebtn);
        new_googlebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (v== new_googlebtn) {
                    google_signInButton.performClick();
                }
                    Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
                    startActivityForResult(intent, SIGN_IN);

            }
        });

1
投票

对于 kotlin 你可以这样做。

val googleTextView: TextView = SignInButton.getChildAt(0) as TextView
    googleTextView.text = "Sign In with Google"

SignInButton 是对您正在使用的按钮 ID 的引用。


0
投票

我鼓励不要使用那些@w.donahue 方法,因为它违反了开放/关闭原则等多项原则。最好的方法是自定义您自己的登录按钮。如果您看到有关登录 Google 的文档,加号按钮只是带有 Textview 的 FrameLayout。通过此链接 https://developers.google.com/+/branding-guidelines#sign-in-button 您有设计按钮的材料。

public class GplusButton extends FrameLayout {

private final String logIn="log in with google +";

private final String logOut="log out";

TextView labelTV;

public GplusButton(Context context) {
    super(context, null);
}

public GplusButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    setBackgroundResource(R.drawable.btn_g_plus_signin_normal);
    addTextLabel();
}

public void addTextLabel() {
    labelTV = new TextView(getContext());
    setTextLogIn();
    labelTV.setTextColor(Color.WHITE);
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    addView(labelTV, params);
}

public void setTextLogIn(){
    labelTV.setText(logIn);
}

public void setTextLogOut(){
    labelTV.setText(logOut);

}

唯一烦人的事情是,即使 Google + 也用 9 补丁扩展标记了 PNG,但它们并不是,所以你必须编辑。

© www.soinside.com 2019 - 2024. All rights reserved.