假設在 RelativeLayout 內放置 1 個 TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
...
...
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="中心對齊" />
</RelativeLayout>
2. TextView1 左邊對齊中垂線
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
...
...
<!-- 看不見的中垂線 -->
<View
android:id="@+id/vCenterVertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/vCenterVertical"
android:text="左邊對齊中垂線" />
</RelativeLayout>
3. TextView1 右邊對齊中垂線
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
...
...
<!-- 看不見的中垂線 -->
<View
android:id="@+id/vCenterVertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@id/vCenterVertical"
android:text="右邊對齊中垂線" />
</RelativeLayout>

沒有留言:
張貼留言