gpt4 book ai didi

android - 如何在 xml 中将我们自己的背景图片设置为 facebook 登录按钮?

转载 作者:行者123 更新时间:2023-11-29 16:00:10 24 4
gpt4 key购买 nike

是否可以在 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 登录按钮的 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 中设置有什么解决办法吗??

最佳答案

请在您的 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();
}

最后在下面的代码中粘贴您的主要 Activity 代码:

 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);

关于android - 如何在 xml 中将我们自己的背景图片设置为 facebook 登录按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25176251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com