首页 文章

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

提问于
浏览
25

我正在将我的应用程序与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+"
  • 其次,我尝试以编程方式设置文本,但是它不起作用 .

任何帮助,将不胜感激 .

编辑

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

7 回答

  • 0

    这是我使用的技术:

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

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

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

    Problem:

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

    Aim

    Google's guidelines

    从文档中,Google建议创建一个自定义按钮,如Customizing the Sign-In Button所述 . 然后它建议使用Sign-In Branding Guidelines中提到的品牌指南 . 这包括使用按钮中的给定自定义图标和图像,设置特定的文本大小,填充和其他为徽标做's and don' ts .

    Clean Solution

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

    • Step 1: 将以下内容添加到 app 模块级 build.gradle 文件中:
    dependencies {
        compile 'com.shobhitpuri.custombuttons:google-signin:1.0.0'
    }
    
    • Step 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="" :像往常一样设置按钮上的文本 .

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

    Source

    希望它可以帮助某人 .

  • 0

    android:text 将无效,因为Google的登录按钮是 FrameLayout 但不是 Button .

    由于text属性仅适用于表示文本格式但不适用于ViewGroups的视图,因此您的解决方案无效 .

    您可以实现的唯一方法是在 w.donahue 中定义 TextView ,如 w.donahue 所述 .

  • 89

    For Beginners

    避免一些崩溃 .

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

    我鼓励不要使用那些@ w.donahue aproach,因为暴力的几个原则是开/关原则 . 最好的apraoch是你自己唱歌按钮 . 如果您在google plus按钮中看到有关Sing的文档,则只是带有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);
    
    }
    

    唯一令人讨厌的事情是,即使谷歌标记9补丁扩展他们不是png,所以你必须编辑

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

相关问题