:
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;
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.
source
share