ConstraintLayout: how to get the view half the width of the screen and in the center?

TD, DR
The width of the presentation should be exactly half the screen and be centered. Using ConstraintLayout.

Note that the view does not have an inner width.

<View android:background="#ff0000" ... />

The original question.
I would like to get a layout where the size of the view is half the size of the screen and horizontally.

Something like this: | --view-- |

I can not find a way to use ConstraintLayout. The best I have found is to use app:layout_constraintHorizontal_weight="1"on 2 fake representations located on the full left and full right, respectively, and app:layout_constraintHorizontal_weight="1.5"on my view.

The best way?

+4
3

- . -, : 25% 75% . 0dp . , 1/2 , .

XML ; - ConstraintLayout, - , .

enter image description here XML

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main_inference"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <View
        android:id="@+id/viewTop"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.5" />


    <android.support.constraint.Guideline
        android:id="@+id/guidelineLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25" />

    <View
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintEnd_toStartOf="@id/guidelineRight"
        app:layout_constraintStart_toEndOf="@id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@id/viewTop" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.75" />

</android.support.constraint.ConstraintLayout>
+4

"ConstraintLayout1.1.0-beta1" .

android:layout_width="0dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent=".4"

, 40% . , .

+1



:
XML

    <View
        android:id="@+id/viewV1"
        android:layout_height="match_parent"
        android:background="#ff0000"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="match_parent"
        />

java.
.

    import android.graphics.Point;
    import android.support.constraint.ConstraintLayout;
    import android.view.Display;
    import android.view.View;

onCreate java .

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width1 = size.x;
    //int height = size.y;
    View v =  findViewById(R.id.viewV1);        
    ConstraintLayout.MarginLayoutParams params = (ConstraintLayout.MarginLayoutParams) v.getLayoutParams();
    params.width = width1/2; params.leftMargin = width1/4; params.rightMargin = width1/4;
    v.setLayoutParams(params);

. , .

java. XML.

<android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline5"
        app:layout_constraintGuide_begin="411dp"
        android:orientation="vertical"
         />

After choosing this, move this directive to the end of the screen and pay attention to this value app:layout_constraintGuide_begin="411dp". Now, whatever the value, this is the width of the screen.

Add marginStartand marginEndto your submission as a 411/4 dp. (calculate this value, XML is not going to do this).
This will make your center half-width look like a parent. Remember that not every screen has 411dp. This will not work for every phone screen size.

0
source

Source: https://habr.com/ru/post/1683581/


All Articles