Primary/Android
ViewBinding을 사용할 때 layout_margin이 적용되지 않는 오류
해스끼
2020. 12. 13. 23:22
레이아웃 XML 파일
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="10dp"
android:layout_marginVertical="10dp"
android:orientation="vertical">
Java 파일
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAddVocaBinding.inflate(layoutInflater)
setContentView(binding.root)
// ...
}
신기술 ``ViewBinding``을 사용하다 오류를 하나 찾아냈다. XML 파일에서 설정한 ``layout_margin``이 가로, 세로 모두 적용되지 않는 문제이다. 놀랍게도 구글링해서 답을 찾을 수 없었다. ``Fragment``에서 오류난 사람은 있었지만 ``Activity``에서 오류가 난 사람은 없었던 듯 하다.
해결책은 그냥 ``layout_margin``을 ``padding``으로 대체하면 된다. 참 쉽죠?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:orientation="vertical">