android:textAlignment
과 android:gravity
의 차이점은 무엇입니까?Android에서 텍스트/레이아웃 맞춤 (텍스트 정렬, 중력)
답변
필자가 볼 수있는 것은 textAlignment가 View 클래스의 멤버이며 중력이 TextView 클래스의 멤버라는 것입니다. 따라서 TextView 및 그 하위 클래스의 경우 중력을 사용할 수있는 반면 모든 뷰에 대해 textAlignment를 사용할 수 있습니다.
TextView와 그 하위 클래스는 텍스트 정렬 기능이 더 필요하므로 중력에 더 많은 옵션이 있다는 것을 알 수 있도록 textAlignment에는 기본적인 옵션 만 있습니다. 그 차이에 대한 명확한 문서를 찾지 못했기 때문에 그것은 나의 추측 일뿐입니다.
textAlignment 및 gravity과 같은 두 가지 문서 링크를 볼 수 있습니다.
지금까지 textAlignment는 주로 사용되지 않는 것으로 보입니다. 설명에 따르면 오른쪽 또는 왼쪽 정렬 만 수행하면됩니다. 중력이 향상된 textAlignment 인 것 같습니다.
API 15를 사용하면 android:textAlignment
에 원하는 결과가 없을 수 있습니다. 아래의 스 니펫은 android:textAlignment="center"
을 사용하여 첫 번째 TextView
객체를 중앙에 배치하려고 시도합니다. 두 번째는 android:gravity="center_horizontal"
을 사용합니다. textAlignment
은 효과가 없지만 중력은 잘 작동합니다. API 17+를 사용하면 textAlignment
텍스트가 예상대로 가운데에 맞춰집니다.
텍스트가 모든 출시와 올바르게 정렬되어 있는지 확인하려면 중력을 고려해야합니다.
는 17 + API의 레이아웃을 결과 :
<LinearLayout
android:layout_width="50dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Fri"
android:textAlignment="center"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="29"
android:gravity="center_horizontal"
android:textSize="18sp" />
</LinearLayout>
결과의 레이아웃