如何自定义风格的LoginButton的Facebook,但仍然正确登录



我使用的是Facebook SDK 4.16.1,我想知道,而不是使用facebook login button的默认样式,我将如何自定义其样式?我想使用一个图标,而不是一个长按钮或编辑其文本,如果我将使用长按钮。如何做到这一点?我试着在我的onCreate中使用下面的东西,但它不像它应该的那样工作,它去了onActivityResult,但忽略了我的FacebookCallback。也许我应该仍然使用<com.facebook.login.widget.LoginButton>,但我将如何改变风格?

facebookButton = (ImageButton) findViewById(R.id.buttonFacebookLogin);
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    com.facebook.AccessToken accessToken = loginResult.getAccessToken();
                    Profile profile = Profile.getCurrentProfile();
                    FB_TOKEN=loginResult.getAccessToken().getToken();
                    id = profile.getId();
            fname = profile.getFirstName();
            lname = profile.getLastName();
            name = profile.getName();
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {

                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            response.getError();
                            Log.e("JSON-RESULT:", object.toString());
                            JSONObject jsonObject = null;
                            if (response.getError() != null) {
                                Toast.makeText(getApplicationContext(), response.getError().getErrorMessage(), Toast.LENGTH_SHORT).show();
                            }else {
                                try {
                                    jsonObject = new JSONObject(object.toString());
                                    email = jsonObject.getString("email");
                                    gender = jsonObject.getString("gender");
                                    locale = jsonObject.getString("locale");
                                    verified = jsonObject.getString("verified");
                                    Log.e(TAG, email + " This is the email");
                                    saveToDB();
                                    Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale");
            request.setParameters(parameters);
            request.executeAsync();
                }
                @Override
                public void onCancel() {
                }
                @Override
                public void onError(FacebookException exception) {
                    Log.e("onError", exception.getMessage());
                }
            });
    facebookButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LoginManager.getInstance().logInWithReadPermissions(SocialNetworkLoginActivity.this, Arrays.asList(Constants.FACEBOOK_PERMISSIONS));
        }
    });

我的Constants.FACEBOOK_PERMISSIONS是这样的

public static final String[] FACEBOOK_PERMISSIONS = new String[] {
                                                                "public_profile",
                                                                "user_friends",
                                                                "email" };

我的布局是这样的

  <ImageButton
                android:id="@+id/buttonFacebookLogin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_socialnetwork_facebook"
                android:contentDescription="@string/content_desc_facebook"
                android:src="@drawable/button_image_facebook" />

我使用了@Rahim Rahimov提供的关于如何自定义LoginButton样式的答案。这是一个很好的示例。

检查我的onActivityResult,我输错了我的意图。

最新更新