Android选择器绘制不重叠

时间:2013-09-30 16:44:09

标签: android android-linearlayout

我的问题是,当我的CheckableLinearLayout被选中时,我从选择器中抽取的图片不会与未经检查的图片重叠。

我的CheckableLinearLayout

public class CheckableLinearLayout extends LinearLayout implements Checkable {
    private boolean mChecked;
    private static final String TAG = CheckableLinearLayout.class
            .getCanonicalName();
    private static final int[] CHECKED_STATE_SET = 
        { android.R.attr.state_checked };

    public CheckableLinearLayout(final Context context) {
        super(context);
        setClickable(true);
        setLongClickable(true);
    }

    public CheckableLinearLayout(final Context context, 
                                 final AttributeSet attrs) {
        super(context, attrs);
        setClickable(true);
        setLongClickable(true);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public CheckableLinearLayout(final Context context,
                                 final AttributeSet attrs, 
                                 final int defStyle) {
        super(context, attrs, defStyle);
        setClickable(true);
        setLongClickable(true);
    }

    @Override
    public void setChecked(final boolean checked) {
        mChecked = checked;
        refreshDrawableState();
    }

    @Override
    protected int[] onCreateDrawableState(final int extraSpace) {
        final int[] drawableState = 
            super.onCreateDrawableState(extraSpace + 1);
        if (isChecked())
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        return drawableState;
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        final Drawable drawable = getBackground();
        if (drawable != null) {
            final int[] myDrawableState = getDrawableState();
            drawable.setState(myDrawableState);
            invalidate();
        }
    }

    @Override
    public boolean performClick() {
        Toast.makeText(getContext(), "click", Toast.LENGTH_SHORT).show();
        if (isChecked()) {
            toggle();
            return false;
        } else
            return super.performClick();

    }

    @Override
    public boolean performLongClick() {
        toggle();
        Toast.makeText(getContext(), "long click", Toast.LENGTH_SHORT).show();
        return super.performLongClick();
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public Parcelable onSaveInstanceState() {
        // Force our ancestor class to save its state
        final Parcelable superState = super.onSaveInstanceState();
        final SavedState savedState = new SavedState(superState);
        savedState.checked = isChecked();
        return savedState;
    }

    @Override
    public void onRestoreInstanceState(final Parcelable state) {
        final SavedState savedState = (SavedState) state;
        super.onRestoreInstanceState(savedState.getSuperState());
        setChecked(savedState.checked);
        requestLayout();
    }

    // /////////////
    // SavedState //
    // /////////////
    private static class SavedState extends BaseSavedState {
        boolean checked;
        @SuppressWarnings("unused")
        public static final Parcelable.Creator<SavedState> CREATOR;
        static {
            CREATOR = new Parcelable.Creator<SavedState>() {
                @Override
                public SavedState createFromParcel(final Parcel in) {
                    return new SavedState(in);
                }

                @Override
                public SavedState[] newArray(final int size) {
                    return new SavedState[size];
                }
            };
        }

        SavedState(final Parcelable superState) {
            super(superState);
        }

        private SavedState(final Parcel in) {
            super(in);
            checked = (Boolean) in.readValue(null);
        }

        @Override
        public void writeToParcel(final Parcel out, final int flags) {
            super.writeToParcel(out, flags);
            out.writeValue(checked);
        }

        @Override
        public String toString() {
            return TAG + ".SavedState{"
                    + Integer.toHexString(System.identityHashCode(this))
                    + " checked=" + checked + "}";
        }
    }
}

我的 layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topics_preview_main_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/favorites_header_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="2dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/favorites_header_title"
            style="@style/CardText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:background="@drawable/card"
            android:gravity="left"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingTop="10dp"
            android:text="@string/favorites_header" />

        <ui.CheckableLinearLayout
            android:id="@+id/favorites_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:background="@drawable/card_selector_w_shadow"
            android:clickable="true"
            android:longClickable="true"
            android:orientation="vertical"
            android:paddingBottom="16dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="8dp"
                android:paddingRight="8dp" >

                <TextView
                    android:id="@+id/favorites_title"
                    style="@style/CardTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="title" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="4dp"
                android:background="@color/C_Favorites_Pink" />

            <LinearLayout
                android:id="@+id/favorites_description_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:padding="4dp" >

                <TextView
                    android:id="@+id/favorites_description"
                    style="@style/CardText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:ellipsize="end"
                    android:maxLines="4"
                    android:text="Lorem ipsum ..." />
            </LinearLayout>
        </ui.CheckableLinearLayout>
    </LinearLayout>

</FrameLayout>

card_selector_w_shadow - &GT;此抽奖将调用选择器

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="2dp" />

            <solid android:color="#ccc" />
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="2dp" />

            <solid android:color="@android:color/white" />

            <padding
                android:bottom="8dp"
                android:left="8dp"
                android:right="8dp"
                android:top="8dp" />
        </shape>
    </item>
    <item android:drawable="@drawable/drawable_states"/>

</layer-list>

我的 drawable_states

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/checked_background" 
          android:state_checked="true"/>
</selector>

这就是发生的事情:

enter image description here

这就是我的意图:

enter image description here

我认为这是因为我的CheckableLinearLayout只会改变孩子的状态,因此抽签只会与孩子重叠。有什么建议吗?

0 个答案:

没有答案