[英]How to properly design a LinearLayout with TextViews on each side?
我試圖寫一個LinearLayout
用作顯示兩個TextViews
的ListView
一部分。 一個是銀行帳戶名,另一個是余額。 我希望帳戶名稱在布局的左側,余額在右側。
更具體地說,我希望將余額向右對齊,並且左側的剩余空間將用於帳戶名稱,但是我無法弄清楚。 我嘗試使用布局權重,但是它們不起作用。
這是我的代碼:
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="30sp"
android:id="@+id/accountNameTextView"
android:layout_weight="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/accountBalanceTextView"
android:layout_weight="1"/>
</LinearLayout>
但是,發生的情況是兩個TextView都放置在左側,而在右側留有一堆空白。 如何強制第二個TextView在右側?
我發布此問題后,便找到了答案。 謝謝所有關注此內容的人,但問題是LinearLayout寬度是換行內容,因此它將兩個TextView彼此相鄰換行。 通過將其更改為此:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
我能夠使用布局權重來實現所需的設計。
我還略微更改了布局權重,這是我所擁有的:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="30sp"
android:id="@+id/accountNameTextView"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/accountBalanceTextView"
android:gravity="center_vertical"/>
</LinearLayout>
試試這個代碼:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/accountBalanceTextView"
android:textSize="30sp"
android:id="@+id/accountNameTextView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/accountBalanceTextView"
/>
</RelativeLayout>
我建議您改用RelativeLayout
。 這是有關如何在RelativeLayout
執行此操作的示例
<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"
tools:context="${packageName}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="@+id/gcmtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/hello_world" />
</RelativeLayout>
但是,如果您需要使用LinearLayout
進行此操作,則可以執行以下操作
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/gcmtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum=2>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="tv1" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/tv2" />
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.