6

我在 Android Studio 中遇到了布局预览的奇怪行为。我开发了一个扩展 AppCompatButton 的自定义视图,并且布局是运行时的预期布局,但是,布局预览中的布局显示不正确。特别是,显示按钮,但没有从自定义属性设置背景颜色,也没有 drawableStart(根据名为“social”的自定义属性在自定义视图初始化中设置)。有关其他详细信息,请参阅下面的代码。请注意,自定义视图位于作为模块导入主项目的库中。我在这里想念什么?谢谢 :)

正确的布局 编辑器(坏)布局

自定义视图代码:

public class SocialButton extends AppCompatButton {
    private static final String TAG = SocialButton.class.getSimpleName();
    Context mContext;
    int backgroundColor;
    int socialId;
    int textColor;

    public SocialButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.SocialButton,
                0, 0);
        initDefaults(a);
        setIconForSocial();
        initStateListDrawable();
    }

    public void initDefaults(TypedArray a){
        try {
            backgroundColor = a.getColor(R.styleable.SocialButton_backgroundColor, Color.WHITE);
            textColor = a.getColor(R.styleable.SocialButton_textColor,
                    ContextCompat.getColor(mContext,R.color.social_button_text_color));
            socialId = a.getInt(R.styleable.SocialButton_social, 0);
        } finally {
            a.recycle();
        }
    }

    public void initStateListDrawable(){
        StateListDrawable stateListDrawable = new StateListDrawable();
        GradientDrawable drawable;
        // Pressed, focused
        drawable =  new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setColor(ContextCompat.getColor(mContext,R.color.border_button_ripple_color));
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawable);
        stateListDrawable.addState(new int[]{android.R.attr.state_focused}, drawable);
        // Disabled
        drawable =  new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setColor(ContextCompat.getColor(mContext,R.color.raised_disabled_color));
        stateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, drawable);
        // Normal
        drawable =  new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setColor(backgroundColor);
        stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, drawable);
        setBackground(stateListDrawable);
        setTextColor(textColor);
        setAllCaps(false);
    }

    public void setBackgroundColor(int color){
        backgroundColor = color;
        initStateListDrawable();
    }

    private void setIconForSocial(){
        Drawable img;
        if (socialId == 0){
            img = getContext().getResources().getDrawable( R.drawable.ic_google );
        }else{
            img = getContext().getResources().getDrawable( R.drawable.ic_facebook );
        }
        //setPadding(LayoutUtils.dpToPx(27,mContext),LayoutUtils.dpToPx(27,mContext),
                //LayoutUtils.dpToPx(27,mContext),LayoutUtils.dpToPx(27,mContext));
        setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
    }
}

布局中的自定义视图 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5F5F5"
    android:paddingBottom="16dp"
    tools:context="it.zehus.bitridesharing.login.SignInActivity">

    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/gd40"
        app:layout_constraintDimensionRatio=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:background="#800000FF" />

    <android.support.constraint.Guideline
        android:id="@+id/gd40"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.40" />


    <android.support.constraint.Guideline
        android:id="@+id/gd63"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.63" />

    <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator"
        style="@style/AndroidImageSlider_Magnifier_Oval_Green"
        android:layout_width="395dp"
        android:layout_height="27dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvCaption" />

    <com.zehus.customlayouts.buttons.BorderButton
        android:id="@+id/btCreateAccount"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="12dp"
        android:text="Create an account"
        android:textAllCaps="false"
        android:textSize="16sp"
        app:borderColor="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btFacebookLogin"
        app:textColor="@color/colorPrimary" />


    <com.zehus.customlayouts.buttons.SocialButton
        android:id="@+id/btFacebookLogin"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="12dp"
        app:social="Facebook"
        android:text="@string/continue_with_facebook"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btGoogleLogin" />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/btPhantomFbLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:visibility="gone" />

    <com.zehus.customlayouts.buttons.SocialButton
        android:id="@+id/btGoogleLogin"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="8dp"
        android:text="@string/continue_with_google"
        app:social="Google"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/gd63">
    </com.zehus.customlayouts.buttons.SocialButton>

    <Button
        android:id="@+id/btSignIn"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:textColor="@color/colorPrimary"
        android:layout_marginStart="24dp"
        android:text="Sign in"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tvCaption"
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:layout_marginTop="16dp"
        android:text="Hybrid vs Boost"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/slider" />

</android.support.constraint.ConstraintLayout>
4

0 回答 0