简体   繁体   English

自定义Facebook登录按钮Android

[英]Custom Facebook Login Button Android

To Creating a facebook login button, I followed facebook documentation and after that my login is working perfectly but I want to customize my button for that I tried lot things but nothing working. 在创建Facebook登录按钮时,我遵循了Facebook文档,此后登录工作正常,但是我想为此自定义按钮,因为我尝试了很多事情但没有任何效果。

1) First I tried from xml like this: 1)首先我尝试从xml这样的:

<com.facebook.login.widget.LoginButton
        android:id="@+id/authButton"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="30dp"
        android:padding="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:textColor="@color/white"
        android:background="@drawable/fbbutton_state"
        android:textSize="15sp"
        android:text="Facebook"
        />

2) Created custom class and extended LoginButton. 2)创建自定义类并扩展了LoginButton。

public class CustomLoginButton extends LoginButton {

public CustomLoginButton(Context context, AttributeSet attrs) {
    super(context, attrs)
     this.setBackgroundColor(R.color.com_facebook_likeboxcountview_border_color);
    this.setCompoundDrawablesWithIntrinsicBounds(R.drawable.my_own_icon, 0, 0, 0);
    this.setText("Facebook");
}

}

in this icon color changed but still text . 此图标的颜色已更改,但仍为文本。

3)And finally I tried in facebook sdk, I changed strings and style, it must work but no result 3)最后我在facebook sdk中尝试了一下,我改变了字符串和样式,它必须工作但没有结果

<string name="com_facebook_loginview_log_in_button">Log in via Facebook</string> 


<style name="com_facebook_loginview_default_style" parent="@android:style/Widget.Button">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">45dp</item>
        <item name="android:background">@drawable/com_facebook_button_blue</item>
        <item name="android:textColor">@color/com_facebook_loginview_text_color</item>
        <item name="android:textSize">@dimen/com_facebook_loginview_text_size</item>
        <item name="android:textStyle">bold</item>
        <item name="android:paddingLeft">@dimen/com_facebook_loginview_padding_left</item>
        <item name="android:gravity">center</item>
    </style>

One difference I found in my login button and common SO login buttons is they are like: <com.facebook.widget.LoginButton and mine like this: <com.facebook.login.widget.LoginButton but I think facebook upgraded this thing. 我在登录按钮和常用的SO登录按钮中发现的一个区别是,它们分别是: <com.facebook.widget.LoginButton和我的这样的: <com.facebook.login.widget.LoginButton但我认为Facebook升级了此功能。

In your XML : 在您的XML中:

Give the width and height of com.facebook.login.widget.LoginButton 0 , Place your custom button, give id and all to it, 给出com.facebook.login.widget.LoginButton 0的宽度和高度,放置您的自定义按钮,并为其提供ID和所有内容,

Now go to your class, get a click listener of the custom button, and place this line.. 现在转到您的课程,获取自定义按钮的单击侦听器,然后放置此行。

authButton.performClick();

Create your own simple Button and then use LoginManager for authorization 创建自己的简单Button,然后使用LoginManager进行授权

ArrayList<String> perms = new ArrayList<>();

perms.add("publish_actions");
LoginManager.getInstance().logInWithPublishPermissions(this, perms);

This workaround I had found long ago, add this line of code in your activity: 我很久以前发现的这种解决方法,在您的活动中添加以下代码行:

btnFb.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
btnFb.setBackgroundResource(R.drawable.fb_drawable);

and do not forget to reset the default facebook button text in your xml as below: 并且不要忘记按如下所示在XML中重置默认的Facebook按钮文本:

facebook:com_facebook_logout_text=""
facebook:com_facebook_login_text=""

I hope you are using facebook sdk 4.x 我希望您正在使用Facebook SDK 4.x

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

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