简体   繁体   English

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

[英]Can I edit the text of sign in button on Google?

I am integrating my application with google plus.我正在将我的应用程序与 google plus 集成。 I have installed google play services and signed in to my account.我已经安装了 google play 服务并登录了我的帐户。 Also I could publish and plus one for what ever I want.我也可以发布和加一个我想要的东西。

My problem我的问题

I can't change the text of the sign in button.我无法更改登录按钮的文本。

My code我的代码

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

What I have tried?我试过什么?

  • First, I tried adding this line to the xml首先,我尝试将此行添加到 xml

     android:text="Share on Google+"
  • Secondly, I tried to set the text programmatically, however it didn't work.其次,我尝试以编程方式设置文本,但它不起作用。

Any help would be appreciated.任何帮助,将不胜感激。

Edit编辑

If it is not possible, is there any way so I can use the same google sign in button on another button?如果不可能,有什么办法可以让我在另一个按钮上使用相同的谷歌登录按钮?

Here is the technique that I used:这是我使用的技术:

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

Here is the easiest way that I used:这是我使用的最简单的方法:

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

Problem:问题:

Other answers have mentioned a workaround.其他答案提到了一种解决方法。 The underlying implementation of the button may change any time which would cause the code to break.按钮的底层实现可能随时更改,这会导致代码中断。 I felt uncomfortable trying to use the hacks.尝试使用这些技巧让我感到不舒服。 For a clean solution, you would think that setting android:text on the com.google.android.gms.common.SignInButton in your layout file would do the trick.对于一个干净的解决方案,您会认为在布局文件中的com.google.android.gms.common.SignInButton上设置android:text可以解决问题。 However it turns out that that attribute is not available for SignInButton .但是事实证明,该属性不适用于SignInButton

Aim目标

Google's guidelines谷歌的指导方针

From documentation, Google suggests creating a custom button as mentioned on Customizing the Sign-In Button [Archived now.从文档中,谷歌建议创建一个自定义按钮,如自定义登录按钮[现在存档。 See the archived page here ].请参阅此处的存档页面]。 It suggests using the branding guidelines as mentioned at Sign-In Branding Guidelines .它建议使用登录品牌指南中提到的品牌指南 This includes using the given custom icons and images in the button, setting specific text size, paddings and other do's and don'ts for the logo.这包括在按钮中使用给定的自定义图标和图像,为徽标设置特定的文本大小、填充和其他注意事项。

Clean Solution :清洁解决方案

Doing as per Google's suggestion involves some custom work.按照谷歌的建议做涉及一些定制工作。 I was willing to do that but wanted to create something reusable, so that others won't have to go through this again.我愿意这样做,但想创造一些可重复使用的东西,这样其他人就不必再经历这个了。 That's why I wrote a quick small (4KB) library does that.这就是为什么我写了一个快速的小型(4KB)库来做到这一点。 Feel free to contribute to it for everyone's benefit if you find issues.如果您发现问题,请随时为每个人的利益做出贡献。

  • Step 1: Add the following to your app module level build.gradle file:第 1 步:将以下内容添加到您的app模块级build.gradle文件中:

     dependencies { implementation 'com.shobhitpuri.custombuttons:google-signin:1.1.0' }
  • Step 2: In your XML Layout, have the following:第 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>

Usage用法

  • android:text="{string}" : As usual to set the text on the button. android:text="{string}" :像往常一样在按钮上设置文本。

  • app:isDarkTheme="{Boolean}" : To switch between blue theme and white theme for the button. app:isDarkTheme="{Boolean}" :在按钮的蓝色主题和白色主题之间切换。 The library handles changing of text color and background color.该库处理文本颜色和背景颜色的更改。 It also handles the change of color on button press or button clicks.它还处理按钮按下或按钮单击时的颜色变化。

Source :来源

Hope it helps someone.希望它可以帮助某人。

android:text will not work because of Google's Sign in button is a FrameLayout but not a Button . android:text不起作用,因为 Google 的 Sign in 按钮是FrameLayout而不是Button

Since text property is only meant for Views representing textual format but not for ViewGroups, your solution is not working.由于 text 属性仅适用于表示文本格式的视图,而不适用于 ViewGroup,因此您的解决方案不起作用。

The only way you can achieve is getting the TextView defined inside FrameLayout as explained by w.donahue .您可以实现的唯一方法是在FrameLayout中定义TextView ,如w.donahue 所述

You can use this class I wrote based on the answer of w.donahue that you can find in this page:您可以使用我根据您可以在此页面中找到的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);
        }
    }
}

For Beginners给菜鸟的

Avoiding few crashes.避免几次崩溃。

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

This worked for me:这对我有用:

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

UNMODIFIED MAIN ACTIVITY FILE未修改的主要活动文件

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

            }
        });

UPDATED Main Activity file更新的主要活动文件

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

            }
        });

I encorage not to use those @w.donahue approach since it violates several principles as open/close principle.我鼓励不要使用那些@w.donahue 方法,因为它违反了几个原则作为打开/关闭原则。 Best approach it's to customize your own sign in button.最好的方法是自定义您自己的登录按钮。 If you see documentation about Sign in Google plus button is just a FrameLayout with Textview.如果您看到有关登录 Google 的文档,加号按钮只是带有 Textview 的 FrameLayout。 At this link https://developers.google.com/+/branding-guidelines#sign-in-button you have material to design the button.在此链接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);

}

The only annoying thing is that even Google + mark with 9 patch extension the PNG they aren't, so you have to edit.唯一烦人的事情是,即使是带有 9 个补丁扩展名的 Google + 标记也不是 PNG,因此您必须进行编辑。

For kotlin you can do this.对于 kotlin,您可以这样做。

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

SignInButton is reference to the button id you are using. SignInButton 是对您正在使用的按钮 ID 的引用。

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

相关问题 如何编辑按钮上的文本? - how can I edit the text on a button? 如何添加按钮Google+登录? - how can i add button Google+ Sign-In? 如何在没有谷歌正常登录按钮的情况下建立登录游戏 - How I can build sign-in to play games without google normal sign-in button 如何在谷歌登录按钮android中居中文本 - How to center text in google sign in button android 如何在ActionBar中添加编辑文本字段和按钮? - How I can add Edit Text field and button in ActionBar? 如何在单击按钮时从编辑文本更改焦点? - How can I change focus from edit text on button click? 是否可以在不自己创建新按钮的情况下编辑谷歌登录按钮 - Is it possible to edit google sign in button without creating a new one ourselves 在Android上以编程方式编辑G +登录按钮文本 - Edit G+ Sign in button text programmatically on Android 我无法通过Firebase来使用Google登录。 Google按钮无响应 - I can not get my Google sign-in to work via Firebase. The Google button is just unresponsive 我可以在Android应用程序中统一Google之类的Facebook登录按钮的样式吗? - Can I unify the style of facebook sign in button like Google in android app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM