Программная установка цвета формы для пользовательского вида - PullRequest
0 голосов
/ 12 сентября 2018

Я сделал пользовательский счетчик и пытаюсь программно изменить фон макета в пользовательском представлении. Я много пытался изменить фон макета с помощью этого метода, но он не работает. Как мне установить фон для рисования, как XML-коды?

Заранее спасибо.

Класс ExtendSpinner:

public class ExtendSpinner extends LinearLayout {

    private LinearLayout lay;
    private Spinner spinner;
    private ImageView img;

    public ExtendSpinner(Context context) {
        super(context);
        init(context, null);
    }

    public ExtendSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ExtendSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs){
        View view = inflate(context, R.layout.layout_spinner, this);
        lay = view.findViewById(R.id.spinnerLayout);
        spinner = view.findViewById(R.id.spinner);
        img = view.findViewById(R.id.spinnerButton);

        setShape(lay, GradientDrawable.RECTANGLE, 2, 2, Color.White, Color.Grey);
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setShape(View v, int shapeType, int cornerRadius, int strokeSize, int backgroundColor, int borderColor) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(shapeType);
        shape.setCornerRadius(cornerRadius);
        shape.setColor(backgroundColor);
        shape.setStroke(strokeSize, borderColor);
        v.setBackgroundDrawable(shape);
    }
}

расположение

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinnerLayout"
    android:orientation="horizontal">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="4dp"
        android:gravity="center"
        android:spinnerMode="dropdown"/>

    <ImageView
        android:id="@+id/spinnerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

</LinearLayout>

activity_main.xml, где я использую ExtendSpinner

<com.bvtech.toolslibrary.Layouts.ExtendCoordinatorLayout
    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:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityMain">

    <com.bvtech.toolslibrary.Widget.ExtendSpinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:entries="@array/test_str"
        app:shapeType="rectangle"
        app:textColor="@color/colorAccent"/>

</com.bvtech.toolslibrary.Layouts.ExtendCoordinatorLayout>

1 Ответ

0 голосов
/ 12 сентября 2018

используйте это вместо inflate

LayoutInflater  mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.layout_spinner, this, true);

не проверено, но может работать.

...