如何使用Android中的Facebook sdk 4.7.0和自定义Google Plus登录按钮制作自定义Facebook登录按钮

时间:2015-10-19 04:55:16

标签: android android-studio gradle facebook-login google-plus-signin

我正在使用facebook登录和谷歌加登录构建一个Android应用程序。我发现很难为这两者制作自定义按钮。有没有办法改变它?

我正在使用android studio 1.4并使用gradle依赖来添加g +和fb登录,就像这样

dependencies 
{
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.android.gms:play-services-identity:8.1.0'
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
}

2 个答案:

答案 0 :(得分:3)

可绘制文件夹中的

fblogin_button_selector.xml

UITextField *textField = [UITextField new];
[textField  addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];

- (void)textFieldChanged:(UITextField *)textField {
    NSString *text = textField.text;

    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

    NSInteger maxLegnth = 5;

    if (!position)
    {
        if (text.length > maxLegnth)
        {
            NSRange rangeIndex = [text rangeOfComposedCharacterSequenceAtIndex:maxLegnth];
            if (rangeIndex.length == 1)
            {
                textField.text = [text substringToIndex:maxLegnth];
            }else {
                NSRange rangeRange = [text rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLegnth)];
                textField.text = [text substringWithRange:rangeRange];
            }
        }
    }
}
布局中

自定义FB登录按钮

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle" >
            <corners android:radius="5dp" />
            <solid android:color="@color/com_facebook_button_background_color_pressed" />
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="5dp" />
            <solid android:color="@color/com_facebook_button_background_color" />
        </shape>
    </item>
</selector>

输出:

enter image description here

使用此功能,您还可以实现G +登录按钮。

答案 1 :(得分:1)

对我来说,这个解决方案有效,

xml上的

按钮

<com.facebook.login.widget.LoginButton
        android:id="@+id/button_facebook"
        xmlns:fb="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@android:color/transparent"
        style="@style/FacebookLoginButton"
        android:longClickable="false" />

更改 styles.xml

<style name="FacebookLoginButton">
    <item name="android:textSize">16sp</item>
    <item name="android:layout_marginTop">10dp</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:layout_gravity">center_horizontal</item>
</style>