Facebook Android SDK 4.0中如何设置登录/注销按钮的文本?

21

我的xml长这样

<com.facebook.login.widget.LoginButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fb_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="CONNECT WITH FACEBOOK"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
android:background="@drawable/button_fb_login"/>

并且背景绘制可拉伸的XML是

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape>
        <solid android:color="#4e69a2" />
        <stroke android:width="3dip" android:color="#4e69a2" />
        <padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
    </shape>
</item>

<item>
    <shape>
        <solid android:color="#3b5998" />
        <stroke android:width="3dip" android:color="#3b5998" />
        <padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
    </shape>
</item>

现在在Android Studio的预览屏幕中,它显示如预期。

enter image description here

但是当我在我的手机或模拟器上运行它时,默认情况下会显示“使用Facebook登录”,就像这样:

enter image description here

怎样才能在运行应用程序时得到我的自定义文本?

2个回答

65

只需要相应地更改属性。请参考:此链接

例如:

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

3
这是完美的解决方案。 - Anand Savjani
1
在Java类中呢? - Zin Win Htet
我们可以添加自己的自定义图像吗? - Amna

32

使用 facebook:login_textfacebook:logout_text

 <com.facebook.widget.LoginButton
            android:id="@+id/login_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            facebook:confirm_logout="false"
            facebook:fetch_user_info="true"
            android:text="testing 123"
            facebook:login_text="LOGIN"
            facebook:logout_text="LOGOUT"
            />

如果上述方法不起作用,请尝试在您的values文件夹中添加以下内容

<resources>
<string name="com_facebook_loginview_log_in_button">LOGIN</string>
<string name="com_facebook_loginview_log_out_button">LOGOUT</string>
</resources>

更新:可能的原因

https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/login/widget/LoginButton.java#L599

更新2:可能的解决方案

在你的strings.xml中覆盖那些字符串。

<string name="com_facebook_loginview_log_out_button">Log out</string>
<string name="com_facebook_loginview_log_in_button">Log in</string>
<string name="com_facebook_loginview_log_in_button_long">Log in with Facebook</string>

其他字符串可以在这里找到。


谢谢提供 GitHub 链接。 - Devanshu Dwivedi
这是正确的答案。如果我覆盖了那些字符串,它就能正常工作。 - Mythul
Facebook最近更新了他们的SDK,导致这些答案不再相关。 :( 我已经用新字符串修复了自己的项目,但是答案中的“其他字符串可以在这里找到”链接已经失效了,所以我也会进行修复。 - Tom Pace
由于编辑拒绝者的原因,上面的答案现在又是错误的了。是的,谢谢大家。 - Tom Pace
新的字符串值 https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/main/res/values/strings.xml - stevyhacker
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接