我有一个google登录按钮,这是xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".login">
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:buttonSize="wide"
app:colorScheme="dark" />
</RelativeLayout>
这是我的Java文件
package com.fireflyelectric.felcosfa;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;
public class login extends AppCompatActivity implements
View.OnClickListener {
private static final int RC_SIGN_IN = 9001;
private GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.sign_in_button).setOnClickListener(this);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
SignInButton signInButton = findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_WIDE);
signInButton.setColorScheme(SignInButton.COLOR_DARK);
}
@Override
protected void onStart() {
super.onStart();
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
mGoogleSignInClient.signOut();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result );
}
}
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
final GoogleSignInAccount acct = result.getSignInAccount();
String email = acct.getEmail();
/* Compare email if exist in database */
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
}
我的问题是如何放大?与网站中使用的Google登录按钮相同
这是我应用程序中的2个文件。我不知道我需要配置的其他部分在哪里。也许来自Java的代码继承了xml
我希望有人可以帮助我。我是android prog的新手。
答案 0 :(得分:0)
我有类似的要求,请尝试以下自定义方法:
private void updateGoogleBtnUi() {
// TODO Auto-generated method stub
SignInButton googleButton = (SignInButton)
findViewById(R.id.google_button);
googleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
for (int i = 0; i < googleButton.getChildCount(); i++) {
View v = googleButton.getChildAt(i);
if (v instanceof TextView)
{
TextView tv = (TextView) v;
tv.setTextSize(14);
tv.setTypeface(null, Typeface.NORMAL);
tv.setText("My Text");
tv.setTextColor(Color.parseColor("#FFFFFF"));
tv.setBackgroundDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
tv.setSingleLine(true);
tv.setPadding(15, 15, 15, 15);
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.width = 100;
params.height = 70;
tv.setLayoutParams(params);
return;
}
}
}
希望有帮助。
答案 1 :(得分:0)
请参阅以下代码段。由于SignInButton通过layout_width
参数设置了大小,因此将按钮的wrap_content
设置为app:buttonSize
。
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:buttonSize="wide"
app:colorScheme="dark" />
您可以从以下各项中设置app:buttonSize
:wide
,standard
和icon_only
。
您还可以通过编程方式设置Button的大小:
// Set the dimensions of the sign-in button.
SignInButton signInButton = findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
答案 2 :(得分:0)
使自定义登录按钮看起来像这样,并向其添加侦听器。