2014-08-27 167 views
-1

對不起,如果我的問題聽起來很簡單,但我正在爲列表項目設計一個簡單的佈局,並且我的意圖與佈局兩端的文本查看對齊。我的嘗試如下,但texviews正好相鄰。任何想法,我要去錯了嗎?Android - 對齊佈局末端的視圖

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/ll_finc_calctr_result_label"> 

     <TextView 
      android:id="@+id/tv_finc_calctr_result_label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_alignRight="@+id/ll_finc_calctr_result_label" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@+id/ll_finc_calctr_result_label"> 

      <TextView 
       android:id="@+id/tv_finc_calctr_result" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center"/> 

    </LinearLayout> 

</RelativeLayout> 

回答

0

試試這個..

<?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="wrap_content" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

    <TextView 
     android:id="@+id/tv_finc_calctr_result_label" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="left" 
     android:text="text left" /> 

    <TextView 
     android:id="@+id/tv_finc_calctr_result" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="right" 
     android:text="text right" /> 

</LinearLayout> 
+0

感謝:如果您需要textViews'寬度實際上WRAP_CONTENT(例如,如果他們有一個背景造型等),您可以使用空間達到更精確的行爲另一種選擇可能是比較有用解。我工作就像一個魅力!真的很感激它。你的回答就是答案。 – TokTok123 2014-08-27 11:16:30

+0

@ TokTok123很高興幫助。快樂編碼。 – Hariharan 2014-08-27 11:17:24

0

首先,你需要從第二線性佈局中刪除的alignRighttoRightOf兩個屬性:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/ll_finc_calctr_result_label"> 

     <TextView 
      android:id="@+id/tv_finc_calctr_result_label" 
      android:layout_width="wrap_content" 
      android:text="ss" 
      android:layout_height="wrap_content"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_alignParentRight="true" > 

      <TextView 
       android:id="@+id/tv_finc_calctr_result" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="sse" 
       android:layout_gravity="center"/> 

    </LinearLayout> 

</RelativeLayout> 

這將導致第二在右側的textview。

而且我會建議你不要把兩個線性佈局,他們都包含只是一個單一的TextView:

喜歡的東西:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

    <TextView 
     android:id="@+id/tv_finc_calctr_result_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ss" /> 

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

</RelativeLayout> 

輸出:

enter image description here

+0

感謝您的回覆。您的回覆在某些版面上似乎適用於我,並且在其他版面上失敗。這一定是我如何放置相關佈局的錯。我會更深入地瞭解我的邏輯。雖然謝謝! – TokTok123 2014-08-27 11:18:05

0

Hariharan提供的解決方案非常出色。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:padding="10dp" > 

<TextView 
    android:id="@+id/tv_finc_calctr_result_label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

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

<TextView 
    android:id="@+id/tv_finc_calctr_result" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" /> 

</LinearLayout> 
+0

歡呼的建議,但正如你所說哈里哈蘭的建議是非常神,它適用於我。謝謝。 – TokTok123 2014-08-27 11:19:31