Как создать этот макет, используя ConstraintLayout - PullRequest
0 голосов
/ 19 октября 2018

У меня проблема с созданным этим экраном xml: problem

Максимальная высота RelativeLayout должна составлять 50% от всей высоты экрана.А если высота компоновки тела превышает 50%, относительная высота компоновки должна уменьшиться.Я не могу понять, как установить относительное расположение max_height на 50%.Благодарю.XML-код экрана:

    <?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/guide_line">

        <LogoView
            android:id="@+id/logo_view"
            android:layout_width="match_parent"
            android:layout_height="144dp"
            android:layout_alignParentBottom="true"
            app:lv_textColor="@android:color/white" />

    </RelativeLayout>

    <android.support.constraint.Guideline
        android:id="@+id/guide_line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />


    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guide_line" />

        </android.support.constraint.ConstraintLayout>

Обновление : я добавил указание, но если моя высота размещения рамки будет больше 50%, то относительная высота выкладки должна уменьшиться, но это не так: содержаниеFramelayout выходит за пределы экрана

Ответы [ 3 ]

0 голосов
/ 19 октября 2018

Добавьте Guideline с ориентацией, которую вы хотите.Затем добавьте ограничение для каждого вида этой строкой.

Вы можете установить фиксированный размер Guideline с этими атрибутами - app:layout_constraintGuide_begin или app:layout_constraintGuide_end.

Также, если вы думаетеоб использовании ConstraintLayout, тогда match_constraint(= 0dp) лучше, чем match_parent.В этом случае ограничения с родителем должны быть установлены вместе.

Это завершенный XML-код, который использовал Guideline.

<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:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline_horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LogoView
            android:id="@+id/logo_view"
            android:layout_width="match_parent"
            android:layout_height="144dp"
            android:layout_alignParentBottom="true"
            android:src="@mipmap/ic_launcher" />

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline_horizontal" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

</android.support.constraint.ConstraintLayout>
0 голосов
/ 19 октября 2018

Попробуйте этот код.

activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/fl_container"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LogoView
            android:id="@+id/logo_view"
            android:layout_width="match_parent"
            android:layout_height="144dp"
            android:layout_alignParentBottom="true"
            app:lv_textColor="@android:color/white" />

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    int mainHeight = dm.heightPixels;
    Log.i(TAG, "mainHeight: " + mainHeight);

    FrameLayout flLayout = findViewById(R.id.fl_container);
    flLayout.setMinimumHeight(mainHeight / 2); // <-- Set pixel value, not dp!
}

Я думаю, что это не половина высоты идеально, но такбудет решением, которое вы хотите.

0 голосов
/ 19 октября 2018

Использование Направляющая

<android.support.constraint.Guideline
            android:id="@+id/centerMargin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent=".50"
            app:layout_constraintStart_toStartOf="parent" />
...