Android 如何将我们自己的背景图像设置为xml格式的facebook登录按钮?
是否可以为Facebook登录按钮设置我们自己的xml背景图像 我的项目中的xmlc(即FBDemo) 我将自己的背景信息传递到xml中的Facebbok loginbutton的xml中 但这给了我如下的运行时错误Android 如何将我们自己的背景图像设置为xml格式的facebook登录按钮?,android,facebook,facebook-login,Android,Facebook,Facebook Login,是否可以为Facebook登录按钮设置我们自己的xml背景图像 我的项目中的xmlc(即FBDemo) 我将自己的背景信息传递到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" 我
08-07 06:41:59.492: E/AndroidRuntime(1731): Caused by: java.lang.NumberFormatException: Invalid int: "res/drawable-hdpi/login_with_fb.png"
我认为发生此错误的原因是,带有_fb.png图像的登录_位于FBDemo中,FacebookSDK项目无法找到带有_fb.png图像的可绘制图像登录_
我知道另一种方法,只是在活动中设置背景图像,但我想在xml中设置
有什么解决方案吗???您只想更改FB登录按钮的背景图像或文本吗 用于更改其背景图像(非运行时) 1.将图像复制到FB SDK的drawable mdpi文件夹中 2.在else模式下写入此行(非编辑模式) 3.在代码行下方添加注释和注释
this.setCompoundDrawablesWithIntrinsicBounds(R.drawable.com_facebook_inverse_icon, 0, 0, 0);
4.1在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"
/>
您只想更改FB登录按钮的背景图像或文本吗 用于更改其背景图像(非运行时) 1.将图像复制到FB SDK的drawable mdpi文件夹中 2.在else模式下写入此行(非编辑模式) 3.在代码行下方添加注释和注释
this.setCompoundDrawablesWithIntrinsicBounds(R.drawable.com_facebook_inverse_icon, 0, 0, 0);
4.1在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"
/>
您应该将其更改为默认按钮并在onButtonClick中调用facebook。您应该将其更改为默认按钮并在onButtonClick中调用facebook。请在您的facebook SDK中更改以下代码:转到com.facebook.widget.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);
}
}
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();
}
另请在该方法的下面一行进行注释:
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);
请更改Facebook SDK中的以下代码:转到com.Facebook.widget.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);
}
}
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();
}
另请在该方法的下面一行进行注释:
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);
我已经知道并实现了,但我想从我的项目中设置图像,我不想将图像复制到我已经知道并实现的Facebook SDK,但我想从我的项目中设置图像,我不想将图像复制到Facebook SDK不想使用默认按钮并调用Facebook,Facebook给出按钮那么我为什么要使用默认按钮我不想使用默认按钮并呼叫Facebook,Facebook给出按钮那么我为什么要使用默认按钮