Создание собственного вида с помощью кнопки внутри приводит к появлению белой кнопки в устройствах, предшествующих Lollipop - PullRequest
0 голосов
/ 15 января 2020

Я создаю пользовательский вид, который состоит из структуры кадра с кнопкой, изображением и текстом на нем.

Он отлично работает в Lollipop и более поздних устройствах, но в pre-Lollipop (например, 4.4 ) на устройстве отображается белая кнопка с ошибкой:

public class CustomImageButton extends FrameLayout {
    public CustomImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageButton,0, 0);

        int imageResource = a.getResourceId(R.styleable.CustomImageButton_image_src, 0);
        int textResource = a.getResourceId(R.styleable.CustomImageButton_text_src, 0);

        try {
            Button button = new Button(context);
            this.addView(button, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setWeightSum(1f);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setClipToPadding(false);

            ImageView image = new ImageView(context);
            image.setAdjustViewBounds(true);
            image.setImageResource(imageResource);
            linearLayout.addView(image, new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,0.3f));

            TextView text = new TextView(context);
            text.setGravity(Gravity.CENTER);
            int padding = (int) getResources().getDimension(R.dimen.spacing_medium);
            text.setPadding(padding,padding,padding,padding);
            text.setText(context.getText(textResource));
            text.setAllCaps(true);
            text.setTypeface(null, Typeface.BOLD);
            linearLayout.addView(text, new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,0.7f));

            this.addView(linearLayout, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                button.setStateListAnimator(null);
                button.setElevation(2f);
                button.setTranslationZ(4f);

                linearLayout.setElevation(2f);
                linearLayout.setTranslationZ(4f);

                image.setElevation(10f);
                image.setTranslationZ(10f);
            }

        } finally {
            a.recycle();
        }
    }
}

Я пытался сделать это:

new ContextThemeWrapper(context, R.style.Widget_AppCompat_Button);

И это:

TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
button.setBackgroundResource(outValue.resourceId);

Но я не могу найдите решение.

Для использования представления вы должны сделать следующее:

<com.package.CustomImageButton
    android:id="@+id/achievementsButton"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:clipToPadding="false"
    android:padding="@dimen/spacing_small"
    app:layout_constraintBottom_toTopOf="@+id/exitButton"
    app:layout_constraintEnd_toStartOf="@+id/ballImageView"
    app:layout_constraintHeight_percent="0.18"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/managerButton"
    app:layout_constraintWidth_percent="0.27"
    custom:image_src="@drawable/ball"
    custom:text_src="@string/achievements" />

И указать это в атрибутах. xml:

   <declare-styleable name="CustomImageButton">
        <attr name="image_src" format="reference" />
        <attr name="text_src" format="reference" />
    </declare-styleable>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...