問題:
我試圖將這兩個 TextView 水平居中,但似乎找不到解決方案。

<TextView
android:id="@ id/have_account_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/register_btn"
android:layout_marginTop="15dp"
android:text="Already have an account? "
android:textColor="@color/black" />
<TextView
android:id="@ id/login_now_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textFontWeight="700"
android:textColor="@color/main_blue"
android:layout_below="@ id/register_btn"
android:layout_marginTop="15dp"
android:layout_toRightOf="@ id/have_account_tv"
android:text="Log in now" />
解決方案
我已經使用 LinearLayout 包裝兩個 TextView 解決了這個問題,然后設定 centerHorizo??ntal="true"。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_below="@ id/login_btn">
<TextView
android:id="@ id/havent_register_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haven't Registered? "
android:textColor="@color/black" />
<TextView
android:id="@ id/login_now_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textFontWeight="700"
android:textColor="@color/main_blue"
android:layout_alignBottom="@ id/havent_register_tv"
android:layout_toRightOf="@ id/havent_register_tv"
android:text="Register Here"/>
</LinearLayout>
uj5u.com熱心網友回復:
嘗試將以下屬性添加到 TextView XMLs
android:layout_centerHorizontal="true"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383944.html
標籤:安卓 xml 文本视图 android-相对布局
