简体   繁体   English

Facebook登录按钮:应用自定义样式

[英]Facebook Login button: apply custom style

i'm facing a strange problem by using new Facebook android sdk 4. With older sdk version i was using: 我使用新的Facebook android sdk 4面临一个奇怪的问题。使用较旧的sdk版本我使用:

 <com.facebook.login.widget.LoginButton
    xmlns:fb="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fb_button"
    style="@style/FacebookLoginButton"
    android:layout_width="485dp"
    android:layout_height="64dp"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="-17dp"
    fb:login_text="@string/login_with_facebook"
    fb:logout_text="Logout" />

FacebookLoginButton section of style.xml looks like style.xml的FacebookLoginButton部分看起来像

<style name="FacebookLoginButton">
    <item name="android:background">@drawable/button_facebook</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">21sp</item>
    <item name="android:gravity">center</item>
</style>

drawable/button_facebook contains png of a custom button shape. drawable / button_facebook包含自定义按钮形状的png。 Now whit old version of sdk everithing works well, but with new sdk something gone wrong. 现在老版本的sdk everithing工作得很好,但是新的sdk出了问题。 i obtain this: 我得到这个:

在此输入图像描述

as you can see, theres two facebook icon: the circled one is of facebook button, the second one (the big one) belongs to button_facebook drawable). 正如你所看到的,两个facebook图标:带圆圈的一个是facebook按钮,第二个(大一个)属于button_facebook drawable)。 There's a way to hide the circled icon? 有一种方法可以隐藏带圆圈的图标吗?

In the latest Facebook v4 API this is the correct answer: 在最新的Facebook v4 API中,这是正确的答案:

<com.facebook.login.widget.LoginButton
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    facebook:com_facebook_login_text="LOGIN"
    facebook:com_facebook_logout_text="LOGOUT"/>

Even i faced the same issue while i was working with fb login.... I fixed the issue by adding the following code.... 即使我在使用fb登录时遇到了同样的问题....我通过添加以下代码解决了这个问题....

    fbLoginButton.setBackgroundResource(R.drawable.facebook);
    fbLoginButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
    fbLoginButton.setCompoundDrawablePadding(0);
    fbLoginButton.setPadding(0, 0, 0, 0);
    fbLoginButton.setText("");

and here is my xml layout: 这是我的xml布局:

<com.facebook.widget.LoginButton
            xmlns:fb="http://schemas.android.com/apk/res-auto"
            android:id="@+id/fbLoginButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            fb:login_text=""
            fb:logout_text=""
            android:scaleType="centerInside" />

Hope it helps you. 希望它能帮到你。

EDIT 1: 编辑1:
Facebook might change the location of LoginButton class which is present inside its SDK so u might need to change the XML tag accordingly. Facebook可能会更改其SDK中存在的LoginButton类的位置,因此您可能需要相应地更改XML标记。 In my case it was inside com.facebook.widget.LoginButton double check it. 在我的情况下,它是在com.facebook.widget.LoginButton里面com.facebook.widget.LoginButton检查它。

您可以使用android:drawableLeft="@null"来摆脱小Facebook图标

<FrameLayout
    android:id="@+id/facebook_lay"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="1"
    android:background="@drawable/facebook_button_bg">

    <com.lovetohave.love.ui.LoveTextView
        android:id="@+id/facebook_txt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:gravity="center"
        android:text="@string/facebook"
        android:textColor="@color/white"
        android:textSize="@dimen/material_small"
        app:customFont="fonts/Roboto-Medium.ttf" />

    <com.rey.material.widget.ProgressView
        android:id="@+id/fb_progress_view_cir"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:visibility="gone"
        app:cpd_strokeColor="#fff"
        app:cpd_strokeSize="1dp"
        app:pv_autostart="true"
        app:pv_circular="true"
        app:pv_progressMode="indeterminate"
        app:pv_progressStyle="@style/CircularProgress1" />

</FrameLayout>

You can design like textview or button .and add following code inside button click event. 你可以设计像textview或button。并在按钮点击事件中添加以下代码。

LoginManager.getInstance().logOut();
LoginManager.getInstance().logInWithReadPermissions(SignIn.this, permissionNeeds);

Then put following code in onCreate method to get profile info 然后将以下代码放在onCreate方法中以获取配置文件信息

LoginManager.getInstance().registerCallback(callbackManager,
    new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            mAccessToken = loginResult.getAccessToken()
                    .getToken();
            PrefUtil.saveData("accesstoken",
                    mAccessToken, SignIn.this);
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {

                        @Override
                        public void onCompleted(org.json.JSONObject object,
                                                GraphResponse response) {


                            } catch (org.json.JSONException e) {
                                // TODO Auto-generated catch block

                                e.printStackTrace();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    });
            Bundle parameters = new Bundle();
            parameters
                    .putString(
                            "fields",
                            "id,name,email,gender,birthday,first_name,last_name,location,picture");
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {
            Log.i(TAG, "LoginManager FacebookCallback onCancel");
            mFbProgressBar.setVisibility(View.GONE);
            mFacebookTxt.setVisibility(View.VISIBLE);
            if (pendingAction != PendingAction.NONE) {
                showAlert();
                pendingAction = PendingAction.NONE;
            }
        }

        @Override
        public void onError(FacebookException exception) {
            mFbProgressBar.setVisibility(View.GONE);
            mFacebookTxt.setVisibility(View.VISIBLE);
            Log.i(TAG, "LoginManager FacebookCallback onError");
            if (pendingAction != PendingAction.NONE
                    && exception instanceof FacebookAuthorizationException) {
                showAlert();
                pendingAction = PendingAction.NONE;
            }
        }

        private void showAlert() {
            new AlertDialog.Builder(SignIn.this)
                    .setTitle(R.string.cancelled)
                    .setMessage(R.string.permission_not_granted)
                    .setPositiveButton(R.string.ok, null).show();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM