0

我很抱歉,我知道這是一個簡單的問題,但我現在無法做到這一點。文字始終處於中心位置,如何?

我想要一個文本總是在中間,另一個在右邊。

但是,當文本太長時間,他們會彼此對話。

假設我希望第二個文本始終位於右側,並且第一個文本位於中間,但是當其中一個文本太長時,居中的文本必須向左移動。

我該怎麼做?

我寫這個,但它不能正常工作。

<RelativeLayout 
     android:id="@+id/workout_footer_text" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/workout_header_height" 
     android:background="@color/green" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin"> 


     <TextView 
      android:id="@+id/review_comments_label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/text_color_white" 
      android:textSize="@dimen/review_top_text_size" 
      android:singleLine="true" 
      android:gravity="center" 
      android:layout_toLeftOf="@+id/review_write_label" 
      android:layout_centerInParent="true" 
      android:layout_gravity="center"/> 


     <TextView 
      android:id="@+id/review_write_label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/text_color_white" 
      android:layout_alignParentRight="true" 
      android:gravity="center_vertical" 
      android:singleLine="true" 
      android:layout_marginLeft="20dp" 
      android:textSize="@dimen/review_top_text_size" 
      android:layout_centerVertical="true"/> 
     </RelativeLayout> 
+0

聽起來像是'maxWidth'可以與下面的答案(S)組合使用。 – stkent

回答

0

而不是在你的第一個TextView的layout_toLeftOf="@+id/review_write_label",刪除該行並添加layout_toRightOf="@id/review_comments_label"到你的第二個TextView的,以確保第一視圖保持居中,第二視圖保持向右。

更新

然後使第二視圖的寬度匹配父和設置它的重力的權利。這將使容器中的文本右對齊,這要歸功於match_parent寬度,該容器從居中的TextView的右邊緣延伸到父邊的右邊緣。

+0

但在這種情況下,文字並不總是處於正確的位置。 – pmb

+0

@pmb看看我的編輯。如果您需要使用.ore代碼,我可以稍後添加它,但我使用手機和編碼從那裏開玩笑 – zgc7009

0

你可以試試這個:

<LinearLayout 
      android:id="@+id/linearLayout2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentRight="true" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="text1" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="text2" />     
     </LinearLayout> 
相關問題