簡體   English   中英

我可以在 Google 上編輯登錄按鈕的文字嗎?

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

我正在將我的應用程序與 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+"
  • 其次,我嘗試以編程方式設置文本,但它不起作用。

任何幫助,將不勝感激。

編輯

如果不可能,有什么辦法可以讓我在另一個按鈕上使用相同的谷歌登錄按鈕?

這是我使用的技術:

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

這是我使用的最簡單的方法:

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

問題:

其他答案提到了一種解決方法。 按鈕的底層實現可能隨時更改,這會導致代碼中斷。 嘗試使用這些技巧讓我感到不舒服。 對於一個干凈的解決方案,您會認為在布局文件中的com.google.android.gms.common.SignInButton上設置android:text可以解決問題。 但是事實證明,該屬性不適用於SignInButton

目標

谷歌的指導方針

從文檔中,谷歌建議創建一個自定義按鈕,如自定義登錄按鈕[現在存檔。 請參閱此處的存檔頁面]。 它建議使用登錄品牌指南中提到的品牌指南 這包括在按鈕中使用給定的自定義圖標和圖像,為徽標設置特定的文本大小、填充和其他注意事項。

清潔解決方案

按照谷歌的建議做涉及一些定制工作。 我願意這樣做,但想創造一些可重復使用的東西,這樣其他人就不必再經歷這個了。 這就是為什么我寫了一個快速的小型(4KB)庫來做到這一點。 如果您發現問題,請隨時為每個人的利益做出貢獻。

  • 第 1 步:將以下內容添加到您的app模塊級build.gradle文件中:

     dependencies { implementation 'com.shobhitpuri.custombuttons:google-signin:1.1.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>

用法

  • android:text="{string}" :像往常一樣在按鈕上設置文本。

  • app:isDarkTheme="{Boolean}" :在按鈕的藍色主題和白色主題之間切換。 該庫處理文本顏色和背景顏色的更改。 它還處理按鈕按下或按鈕單擊時的顏色變化。

來源

希望它可以幫助某人。

android:text不起作用,因為 Google 的 Sign in 按鈕是FrameLayout而不是Button

由於 text 屬性僅適用於表示文本格式的視圖,而不適用於 ViewGroup,因此您的解決方案不起作用。

您可以實現的唯一方法是在FrameLayout中定義TextView ,如w.donahue 所述

您可以使用我根據您可以在此頁面中找到的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);
        }
    }
}

給菜鳥的

避免幾次崩潰。

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

這對我有用:

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

            }
        });

我鼓勵不要使用那些@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);

}

唯一煩人的事情是,即使是帶有 9 個補丁擴展名的 Google + 標記也不是 PNG,因此您必須進行編輯。

對於 kotlin,您可以這樣做。

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

SignInButton 是對您正在使用的按鈕 ID 的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM