I have recently started learning the new ConstraintLayout
in Android Studio 2.2 and noticed that when I add simplest of the views, the layout editor automatical
You'll note that all of the absolute values are in the tools
namespace - this means they are not compiled into your app, nor used in anything but in the tools (and in this case, the visual editor). They are simply to ensure that switching from the Design to Text tab is always consistent, with the underlying files remaining stable.
- Is there a way to tell Android Studio to not generate these?
No.
- Should I manually place them in dimens.xml?
These are only useful for the tools and therefore should not be added to a separate dimens.xml
file that would be included in your final APK.
- Would these absolutes cause some layout problems in other devices?
No, they are only used by the tools.
As a side notes to given answers, you can use the Magic Wand Icon in the toolbar menu above the design preview. Click on Infer Constraints button, this will automatically add some lines in the text field and the tools one will be converted to constrained.
Please see below picture :
I'm not sure your original question contains your entire layout, as it references a widget with an id of @+id/activity
, so the issue might lie elsewhere in your layout.
Ensure that no widget that exists within a ConstraintLayout
has a layout_width
or layout_height
of match_parent
.
MATCH_PARENT is not supported for widgets contained in a ConstraintLayout, though similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
Source
If you use match_parent
, Android Studio will generate these absolute values, as well as replacing match_parent
with an absolute dimension.
Based on the layout you posted, your TextView
probably had a layout_width
or layout_height
of match_parent
before Android Studio replaced it.
You should replace android:layout_width="match_parent"
with
android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndtOf="parent"
And android:layout_height="match_parent"
with
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomtOf="parent"
In your specific layout, you probably want something like this:
<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/activity_portfolio"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.Activity">
<TextView
android:text="@string/creator_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/first_textview"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/activity"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp" />
</android.support.constraint.ConstraintLayout>