是否可以在xml中为Facebook登录按钮设置我们自己的背景图像?
我的项目中的xmlc(即FBDemo)
<com.facebook.widget.LoginButton xmlns:facebook="http://schemas.android.com/apk/res-auto" android:id="@+id/btn_facebook" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btn_login" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" facebook:background_image="@drawable/login_with_fb" facebook:confirm_logout="false" facebook:fetch_user_info="true" />
我在Facebook SDK中进行了这样的更改
在FacebookSDK项目中 值/ attr.xml
<declare-styleable name="com_facebook_login_view"> <attr name="confirm_logout" format="boolean"/> <attr name="fetch_user_info" format="boolean"/> <attr name="login_text" format="string"/> <attr name="logout_text" format="string"/> <attr name="background_image" format="integer"/> </declare-styleable>
并在LoginButton.java中
private int background_image;
private void parseAttributes(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_login_view);
confirmLogout = a.getBoolean(R.styleable.com_facebook_login_view_confirm_logout, true);
fetchUserInfo = a.getBoolean(R.styleable.com_facebook_login_view_fetch_user_info, true);
loginText = a.getString(R.styleable.com_facebook_login_view_login_text);
logoutText = a.getString(R.styleable.com_facebook_login_view_logout_text);
background_image = a.getInt(R.styleable.com_facebook_login_view_background_image, R.drawable.com_facebook_button_blue);
a.recycle();
}
public LoginButton(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs.getStyleAttribute() == 0) {
// apparently there's no method of setting a default style in xml,
// so in case the users do not explicitly specify a style, we need
// to use sensible defaults.
this.setGravity(Gravity.CENTER);
this.setTextColor(getResources().getColor(R.color.com_facebook_loginview_text_color));
this.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.com_facebook_loginview_text_size));
this.setTypeface(Typeface.DEFAULT_BOLD);
if (isInEditMode()) {
// cannot use a drawable in edit mode, so setting the background color instead
// of a background resource.
this.setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
// hardcoding in edit mode as getResources().getString() doesn't seem to work in IntelliJ
loginText = "Log in with Facebook";
} else {
//this.setBackgroundResource(R.drawable.com_facebook_button_blue);
this.setBackgroundResource(background_image);
this.setCompoundDrawablesWithIntrinsicBounds(R.drawable.com_facebook_inverse_icon, 0, 0, 0);
this.setCompoundDrawablePadding(
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_compound_drawable_padding));
this.setPadding(getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_left),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_top),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_right),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_bottom));
}
}
parseAttributes(attrs);
if (!isInEditMode()) {
initializeActiveSessionWithCachedToken(context);
}
}
我将自己的背景传递给xml
中的Facebbok loginbutton的xml但是这给了我下面的运行时错误
08-07 06:41:59.492: E/AndroidRuntime(1731): Caused by: java.lang.NumberFormatException: Invalid int: "res/drawable-hdpi/login_with_fb.png"
我认为发生此错误是因为login_with_fb.png图像驻留在FBDemo中而且FacebookSDK项目中找不到可绘制的图像login_with_fb.png
我知道另一种方式,只是在Activity中设置背景图片,但我想在xml中设置 有没有解决方案??
答案 0 :(得分:4)
请在Facebook SDK中更改以下代码:转到com.facebook.widget.LoginButton.java
public LoginButton(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs.getStyleAttribute() == 0) {
// apparently there's no method of setting a default style in xml,
// so in case the users do not explicitly specify a style, we need
// to use sensible defaults.
this.setGravity(Gravity.CENTER);
this.setTextColor(getResources().getColor(R.color.com_facebook_loginview_text_color));
this.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.com_facebook_loginview_text_size));
this.setTypeface(Typeface.DEFAULT_BOLD);
if (isInEditMode()) {
// cannot use a drawable in edit mode, so setting the background color instead
// of a background resource.
// this.setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
// hardcoding in edit mode as getResources().getString() doesn't seem to work in IntelliJ
loginText = "Log in with Facebook";
} else {/*
this.setBackgroundResource(R.drawable.com_facebook_button_blue);
this.setCompoundDrawablesWithIntrinsicBounds(R.drawable.com_facebook_inverse_icon, 0, 0, 0);
this.setCompoundDrawablePadding(
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_compound_drawable_padding));
this.setPadding(getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_left),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_top),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_right),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_bottom));
*/}
}
parseAttributes(attrs);
if (!isInEditMode()) {
initializeActiveSessionWithCachedToken(context);
}
}
同时从此方法中删除代码:
private void setButtonText() {/*
if (sessionTracker != null && sessionTracker.getOpenSession() != null) {
setText((logoutText != null) ? logoutText :
getResources().getString(R.string.com_facebook_loginview_log_out_button));
} else {
setText((loginText != null) ? loginText :
getResources().getString(R.string.com_facebook_loginview_log_in_button));
}
*/}
同样在此方法的下面注释:
private void parseAttributes(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_login_view);
confirmLogout = a.getBoolean(R.styleable.com_facebook_login_view_confirm_logout, true);
fetchUserInfo = a.getBoolean(R.styleable.com_facebook_login_view_fetch_user_info, true);
/* loginText = a.getString(R.styleable.com_facebook_login_view_login_text);
logoutText = a.getString(R.styleable.com_facebook_login_view_logout_text);*/
a.recycle();
}
最后在主要活动代码中粘贴代码:
loginButton = (LoginButton) findViewById(R.id.btn_facebook);
loginButton.setReadPermissions(Arrays.asList("email","public_profile"));
loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
WelcomeActivity.this.user = user;
updateUI();
}
});
loginButton.setBackgroundResource(R.drawable.btn_splash_fb);
答案 1 :(得分:0)
您只想更改FB登录按钮背景图片或文字吗?
用于更改其背景图像(不是运行时)
1.将您的图像复制到FB SDK的drawable-mdpi文件夹。
2.在else模式下编写此行(不是编辑模式)
this.setBackgroundResource(R.drawable.yourImageName);
3.并在代码
下面注释 this.setCompoundDrawablesWithIntrinsicBounds(R.drawable.com_facebook_inverse_icon, 0, 0, 0);
4.和你的xml
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
facebook:background_image="@drawable/login_with_fb"
facebook:confirm_logout="false"
facebook:fetch_user_info="true"
android:textStyle="bold"
/>
答案 2 :(得分:0)
您应该将其更改为默认按钮并在onButtonClick中调用facebook。