App/Android
constraintlayout weight
selfstarter
2020. 6. 20. 17:23
constraintlayout weight
constraintlayout weight를 하기위해선 위젯들은 chain으로 묶어야 한다.
chain화 시키기 위해선 위젯들 서로 관계성을 지녀야 한다. 방법은 다음과 같다
-
위젯들의 양옆의 관계를 설정해준다(난 수평에 비율을 주고 싶으므로 start, end에 다른 위젯,parent를 연결시켰다)
-
수평에 비율을 주고 싶으므로 layout_width에 0dp를 준다
-
layout_constraintHorizontal_weight으로 각 위젯의 비율을 설정한다
<?xml version="1.0" encodin g="utf-8"?> <androidx.constraintlayout.widget.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" tools:context=".MainActivity"> <EditText android:id="@+id/bought_item_price_edit_text" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toStartOf="@id/bought_item_date" app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" app:layout_constraintHorizontal_weight="80" android:contentDescription="bought item price" /> <TextView android:id="@+id/bought_item_date" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toEndOf="@id/bought_item_price_edit_text" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" app:layout_constraintHorizontal_weight="20" android:text="테스트테스트" /> </androidx.constraintlayout.widget.ConstraintLayout>