[英]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.