2010-01-20 136 views
128

我正在開始使用Android。我無法獲得簡單的佈局。在Android佈局中對齊左側和右側邊的文字瀏覽

我想用一個LinearLayout在單排定位兩個TextViews。一個TextView在左側,另一個在右側(類似於float:left,float:right在CSS中)。

這是可能的,或者我需要使用不同的ViewGroup或進一步佈局嵌套來完成呢?

這是我到目前爲止有:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:orientation="horizontal" android:padding="10sp"> 
<TextView android:id="@+id/mytextview1" android:layout_height="wrap_content" android:text="somestringontheleftSomestring" android:layout_width="wrap_content"/> 
<TextView android:id="@+id/mytextview2" android:layout_height="wrap_content" android:ellipsize="end" 
    android:text="somestringontheright" android:layout_width="wrap_content"/> 
</LinearLayout> 

回答

243

使用RelativeLayoutlayout_alignParentLeftlayout_alignParentRight

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:id="@+id/mytextview1"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:id="@+id/mytextview2"/> 

</RelativeLayout> 

此外,you should probably be using dip (or dp) rather than sp in your layoutsp反映文本設置以及屏幕密度,因此它們通常僅用於調整文本項目大小。

+45

爲了防止重疊的內容,也可能要加上 '機器人:layout_toLeftOf = 「@ + ID/mytextview2」' 以第一的TextView – 2010-11-19 22:11:18

+0

對於動態表,你可以這樣做: TableRow.LayoutParams col1Params = new TableRow.LayoutParams(); \t \t //結束該行的內容 \t \t col1Params.height = LayoutParams。包裝內容; \t \t col1Params.width = LayoutParams.WRAP_CONTENT; \t \t \t \t //設置重力到中心柱的重力 \t \t col1Params.gravity = Gravity.LEFT; – siddhusingh 2014-08-24 10:31:30

+0

謝謝你,你讓我的生活更輕鬆。真正的thx .. – choz 2016-03-27 17:28:02

80

您可以使用gravity屬性來「浮動」視圖。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center_vertical|center_horizontal" 
      android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:layout_weight="1" 
      android:text="Left Aligned" 
      /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_weight="1" 
      android:text="Right Aligned" 
      /> 
    </LinearLayout> 

</LinearLayout> 
+1

不幸的是,它似乎並不會導致兩個項目在同一行上 – Webnet 2013-01-15 04:52:51

+1

重力只會影響「文本對齊」嗎? – 2013-10-13 06:10:28

+0

現在不工作。可能它工作在較舊的API – 2014-06-18 23:50:10

4

<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Hello world" /> 

<View 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Gud bye" /> 

+1

什麼........ – Subho 2016-03-09 10:35:13

+0

我想做的同樣的評論:D:D;) – eRaisedToX 2017-08-07 16:26:36

+0

這種解決方案適用於許多情況下,接受的解決方案不 – slezica 2017-09-04 18:05:45

6

即使Rollin_s的小費,戴夫·韋伯的回答並沒有爲我工作。右邊的TextView中的文字仍然與左邊的TextView中的文字重疊。

我終於得到了我想要的東西這樣的行爲:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout01" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp"> 

<TextView 
    android:id="@+id/mytextview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" /> 

<TextView 
    android:id="@+id/mytextview2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/mytextview1" 
    android:gravity="right"/> 

</RelativeLayout> 

注意mytextview2具有"android:layout_width"設置爲"match_parent"

希望這可以幫助別人!

0

它可以用LinearLayout(較少的開銷)完成。給第二個視圖剩餘空間,以便gravity可以工作。經測試回API 16

Proof

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Aligned left" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="end" 
     android:text="Aligned right" /> 
</LinearLayout> 

如果你想限制第一文本視圖的大小,這樣做:

調整權重的要求。

Proof 2

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Aligned left but too long and would squash the other view" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:gravity="end" 
     android:text="Aligned right" /> 
</LinearLayout> 
相關問題