在Android工作室的自定义视图的编辑器中显示正确的布局

时间:2017-11-23 10:14:27

标签: android android-custom-view android-layout-editor

我在Android Studio中遇到布局预览的奇怪行为。我已经开发了一个扩展AppCompatButton的自定义视图,并且布局是运行时预期的布局,但布局预览中的布局无法正确显示。特别是,显示按钮但没有自定义属性设置的背景颜色,也没有drawableStart(根据名为&#34的自定义属性在自定义视图init中设置;社交")。 有关其他详细信息,请参阅以下代码 请注意,自定义视图位于作为模块在主项目中导入的库中。 我在这里错过了什么?谢谢:))

Right Layout Editor (bad) layout

自定义视图代码:

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>

0 个答案:

没有答案