Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout constraints attribute in style

Tags:

android

How to use constraint attributes in style?

When I'm trying to use it as any other attributes with custom namespace it's has no effect on my view.

 <style name="Header.Center" parent="Header">
    <item name="layout_constraintBottom_toBottomOf">parent</item>
 </style>

Adding namespace app: is not helping.

like image 556
czajna666 Avatar asked Jun 26 '18 18:06

czajna666


People also ask

How many constraint handles are available to us as part of the ConstraintLayout?

The TextView above has three types of handles: Resize handle - It's present on the four corners and is used to resize the view, but keeping its constraints intact. Side handle - It's the circular handle present on the centre of each side. It's used to set the top, left, bottom and right constraints of the view.

Is ConstraintLayout faster than LinearLayout?

Layout with 2 views on different sides. Results show that the fastest layout is Relative Layout, but difference between this and Linear Layout is really small, what we can't say about Constraint Layout. More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.

Can we use ConstraintLayout inside ConstraintLayout?

You can use a ConstraintLayout in an other ConstraintLayout but you need to respect some rules. All direct childs of a ConstraintLayout should have constraint on left,top, right and bottom.


2 Answers

First of all, make sure that the View that you're applying the style on is a direct child of the ConstraintLayout. Otherwise, the constraints will not be taken into account when positioning the View.

I have tried it and the way you tried does in fact work. I have added the following style to the styles.xml:

<style name="CustomStyle">
    <item name="layout_constraintBottom_toBottomOf">parent</item>
    <item name="layout_constraintEnd_toEndOf">parent</item>
</style>

Created a basic layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/CustomStyle"/>

</android.support.constraint.ConstraintLayout>

And it does indeed position the TextView at the bottom right corner of the parent.

like image 67
Pawel Laskowski Avatar answered Oct 04 '22 03:10

Pawel Laskowski


maybe your view's attribute is "match_parent",but it is wrong. It should be "wrap_content".

 <TextView
            android:text="Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/CustomStyle"/>
like image 31
dong liu Avatar answered Oct 04 '22 01:10

dong liu