Размещение кнопки методом - PullRequest
0 голосов
/ 25 декабря 2018

Я хочу поместить кнопку в определенные координаты в ConstraintLayout, где верхний левый угол равен (0, 0).Поэтому я пытаюсь прикрепить кнопку к верхнему левому углу и сделать поля для представления координат.Я сделал несколько попыток, но кнопки все еще помещаются в (0, 0), а XML для кнопок не применяется.

Моя последняя отчаянная попытка для этого метода:

void addButton(int x, int y) {

        try {

            Button nb = (Button) getLayoutInflater().inflate(R.layout.button_layout,null);
            nb.setOnClickListener(new MyOnClick(nb));
            nb.setText(""+count);
            nb.setVisibility(View.VISIBLE);

            ConstraintLayout.LayoutParams lparams = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lparams.leftMargin = x;
            lparams.topMargin = y;

            nb.setLayoutParams(lparams);
            list.putButton(new XYButton(nb,lparams)); //just synthetic merge of button and layout parameters
            front.addView(nb);

        } catch(Exception ee){
            Toast.makeText(Field.this, ee.toString(), Toast.LENGTH_LONG).show(); }
        catch(Error ee){  }
    }

Когда я помещаю имя ConstraintLayout в inflate () вместо null, применяется XML, но он выдает исключение, что ConstraintLayout не может быть приведен к Button.

XML для кнопки:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="40dp"
            android:layout_height="35dp"
            android:layout_gravity="center_horizontal"
            android:text="button"
            android:textSize="15dp"
    />

XML для поля:

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Field" android:visibility="visible" android:id="@+id/back" tools:layout_editor_absoluteY="25dp">

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp" android:id="@+id/linearLayout"
            android:gravity="bottom" android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent">
        <Button
                android:text="@string/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/add"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_weight="1" style="@style/Widget.AppCompat.Button.Borderless"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/rm" android:layout_weight="1"
                style="@style/Widget.AppCompat.Button.Borderless" android:visibility="visible"
                android:text="@string/rm"/>
    </LinearLayout>
    <android.support.constraint.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout" android:id="@+id/front">
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

Что мне изменить?Я не уверен, даже если я пытаюсь сделать это должным образом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...