Как я могу создать "квадрат" ConstraintLayout, который включен в цепочку виджетов?

Я пытаюсь создать "квадрат" ConstraintLayout расширяя ConstraintLayout Класс следующим образом:

class SCConstraintLayout extends android.support.constraint.ConstraintLayout {
public SCConstraintLayout(Context context) {
    super(context);
}

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

public SCConstraintLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int chosenDimension = 0;
    int mode = 0;

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        chosenDimension = MeasureSpec.getSize(heightMeasureSpec);
        mode = MeasureSpec.getMode(heightMeasureSpec);
    } else {
        chosenDimension = MeasureSpec.getSize(widthMeasureSpec);
        mode = MeasureSpec.getMode(widthMeasureSpec);
    }

    int side = MeasureSpec.makeMeasureSpec(chosenDimension, mode);

    super.onMeasure(side, side);
    setMeasuredDimension(side, side);
}
}

Если я включу виджет с этим пользовательским классом в цепочку виджетов, например в следующем альбомно-ориентированном макете

<?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:id="@+id/scMainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_optimizationLevel="chains"
tools:context=".MainActivity"
tools:layout_behavior="@string/appbar_scrolling_view_behavior">

<!-- gridFrame -->
<android.support.constraint.ConstraintLayout
    android:id="@+id/gridFrame"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHorizontal_chainStyle="spread_inside"
    app:layout_constraintVertical_chainStyle="spread_inside"
    app:layout_optimizationLevel="chains"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/Report"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    >

    <!-- column headers  -->
    <android.support.constraint.ConstraintLayout
        android:id="@+id/colHeaders"
        android:layout_width="0dp"
        android:layout_height="14dp"
        android:orientation="horizontal"
        app:layout_constraintVertical_chainStyle="spread_inside"
        app:layout_optimizationLevel="chains"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/SCFrame"
        app:layout_constraintLeft_toLeftOf="@+id/SCFrame"
        app:layout_constraintRight_toRightOf="@+id/SCFrame"
        >

        <TextView
            android:id="@+id/textView0"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="A"
            android:background="#FF0000"
            app:layout_constraintHorizontal_chainStyle="spread_inside"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            />
        </android.support.constraint.ConstraintLayout>

    <!-- row headers  -->
    <android.support.constraint.ConstraintLayout
        android:id="@+id/rowHeaders"
        android:layout_width="14dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_optimizationLevel="chains"
        app:layout_constraintTop_toTopOf="@+id/SCFrame"
        app:layout_constraintBottom_toBottomOf="@+id/SCFrame"
        app:layout_constraintRight_toLeftOf="@+id/SCFrame"
        app:layout_constraintLeft_toLeftOf="parent"
        >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="1"
            android:background="#FF0000"
            app:layout_constraintVertical_chainStyle="spread_inside"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            />
        </android.support.constraint.ConstraintLayout>


        <be.ema.sclibrary.SCConstraintLayout
            android:id="@+id/SCFrame"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#00FF00"
            app:layout_constraintLeft_toRightOf="@+id/rowHeaders"
            app:layout_constraintTop_toBottomOf="@id/colHeaders"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            >

            <TextView
                android:id="@+id/A1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:text="this is A1"
                android:background="#0000FF"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                />
        </be.ema.sclibrary.SCConstraintLayout>
    </android.support.constraint.ConstraintLayout>

<TextView
    android:id="@+id/Report"
    android:layout_width="200dp"
    android:layout_height="0dp"
    android:text="This is the report"
    android:background="#FFFF00"
    app:layout_constraintLeft_toRightOf="@+id/gridFrame"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    />

</android.support.constraint.ConstraintLayout>

и если я показываю его на "Nexus 6" в эмуляторе Android Studio, например, это выглядит как SCFrame только частично квадратный:

  • A1 TextView является квадратным (т.е. его ширина равна его высоте)
  • Однако ширина SCFrame сам по себе отличается от своей высоты

Как я должен изменить SCConstraintLayout класс или XML-макет, чтобы получить SCFrame смотреть идеально квадратный (т.е. его ширина равна его высоте) при отображении на устройстве?

NB: в моем реальном приложении, SCFrame имеет несколько строк и столбцов. И есть несколько других виджетов, таких как кнопки и текстовые представления вместо Report TextView.

1 ответ

Решение

Вам просто нужен верхний уровень, чтобы быть ConstraintLayout. Вложенные и пользовательский класс не нужны. Что касается квадратного TextView, вы можете использовать атрибут аспектного отношения.

Другие вопросы по тегам