我試圖讓兩個TextView
的一個到左側,另一個到右側。
我曾期待layout_gravity
足以做到這一點。
但它不起作用。示例代碼:爲什麼layout_gravity設置爲左/右對水平LinearLayout沒有影響?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LEFT"
android:textSize="14sp"
android:layout_gravity="left"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RIGHT"
android:textSize="14sp"
android:layout_gravity="right"
/>
</LinearLayout>
</LinearLayout>
我在做什麼錯?
這似乎工作,但爲什麼我需要定義重力和體重是多少?我認爲'layout_gravity'設置父級內的位置。 「重力」僅用於文本(內容)。你能解釋一下嗎? – Jim
你想要兩個文本均勻分佈一半和一半;這就是爲什麼我們需要在這裏使用權重。我們還需要將寬度設置爲零(0),因爲方向是水平的。權重將每個項目放在其位置(右側和左側)。重力將物品放置在其所需的位置。 – Eenvincible
我認爲'layout_gravity'正確定位了'TextView'的父權。似乎我誤解了一些東西。 – Jim