Layout Google Sign In Button - Android

2019-02-10 12:19发布

I'm searching everywhere and not finding what I want, I'm starting to think that this is impossible.

Here's the question: I have a google button for sign in, it's a beautiful button, but it doesn't fit in my layout, I would like to use an image, created by me in the google button, like what happens in the Image Buttons, is that possible? If yes, how can I do this?

Thanks

3条回答
对你真心纯属浪费
2楼-- · 2019-02-10 12:49

I have your question solution. I tried this always for custom SignIn Button for google plus.

Try this it is tested by me.

Here is the Code.

xml layout

<com.google.android.gms.common.SignInButton
    android:id="@+id/google_button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:text="Text"
    android:layout_alignParentTop="true"
    android:layout_margin="10dp"
    android:textSize="18sp" />

Method for customisation

private void googleBtnUi() {
    // 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);

            return;
        }
    }
}

Edit

Add this code before return in method googleBtnUi()

ViewGroup.LayoutParams params = tv.getLayoutParams();
params.width = 100;
params.height = 70;
tv.setLayoutParams(params);
查看更多
你好瞎i
3楼-- · 2019-02-10 12:51

There's no need to use Google Sign-In button, if it doesn't fit in your layout. You can simply create your own Button with your own design and in OnClick method just create Google sign in intent.

Button myGoogleBtn = (Button) findViewById(R.id.loginGoogle);
myGoogleBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       Intent signInIntent = mGoogleSignInClient.getSignInIntent();
       startActivityForResult(signInIntent, RC_SIGN_IN); 
    }
});
查看更多
淡お忘
4楼-- · 2019-02-10 12:56

You can create your own button and use XML tag android:drawableLeft="@drawable\file" to show any image on button.

查看更多
登录 后发表回答