2024-06-05

【Android】RelativeLayout 對齊螢幕中垂線

假設在 RelativeLayout 內放置 1 個 TextView

1. TextView 的中心對齊中垂線:
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. xmlns:ads="http://schemas.android.com/apk/res-auto"
  9. tools:context=".MainActivity">
  10.  
  11. ...
  12. ...
  13.  
  14. <TextView
  15. android:id="@+id/TextView1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_centerInParent="true"
  19. android:layout_centerVertical="true"
  20. android:text="中心對齊" />
  21. </RelativeLayout>
  22.  


2. TextView1 左邊對齊中垂線
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. xmlns:ads="http://schemas.android.com/apk/res-auto"
  9. tools:context=".MainActivity">
  10.  
  11. ...
  12. ...
  13. <!-- 看不見的中垂線 -->
  14. <View
  15. android:id="@+id/vCenterVertical"
  16. android:layout_width="0dp"
  17. android:layout_height="match_parent"
  18. android:layout_centerInParent="true" />
  19.  
  20. <TextView
  21. android:id="@+id/TextView1"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_toEndOf="@id/vCenterVertical"
  25. android:text="左邊對齊中垂線" />
  26. </RelativeLayout>
  27.  


3. TextView1 右邊對齊中垂線
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. xmlns:ads="http://schemas.android.com/apk/res-auto"
  9. tools:context=".MainActivity">
  10.  
  11. ...
  12. ...
  13. <!-- 看不見的中垂線 -->
  14. <View
  15. android:id="@+id/vCenterVertical"
  16. android:layout_width="0dp"
  17. android:layout_height="match_parent"
  18. android:layout_centerInParent="true" />
  19.  
  20. <TextView
  21. android:id="@+id/TextView1"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_toStartOf="@id/vCenterVertical"
  25. android:text="右邊對齊中垂線" />
  26. </RelativeLayout>
  27.  




沒有留言:

張貼留言