2015-04-17 42 views
2

我有一個RelativeLayout和兩個元素:一個TextView和一個ImageButton。 ImageButton應該位於TextView的右側,所以我使用android:layout_toRightOf屬性。 這兩個元素的寬度都使用android:layout_width =「wrap_content」。ImageButton右鍵TextView

現在我的問題:只有當TextView中的文本達到屏幕的整個寬度時,它纔會包裝到下一行。但目前,ImageButton已經隱藏在屏幕的右側(因爲由於wrap_content的原因,TextView使用了父級的全部寬度)。

enter image description here

所以我希望做的是說:在ImageButton的常是對的TextView的權利,但它有一個固定大小(可以說100dp),需要這個大小被保留ImageButton,因此只要ImageButton「觸及」屏幕的右側,文本就需要包裝。

我已經嘗試過maxWidth和maxEms,但似乎並不如預期的工作(它總是大小的話,不僅當文本達到最大寬度)

希望你明白我的意思,否則我發帖代碼和截圖!

+0

請發表您的XML。 – tambykojak

回答

3

如果您發佈您的代碼,這將有所幫助。但從我所知道的情況來看,你知道圖像的位置是固定的,所以爲什麼不讓TextView的位置取決於ImageView

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="id/image" /> 
</RelativeLayout> 
+0

這聽起來不錯,所以試試看,並接受爲正確答案。 –

+0

一個好的解決方案,會接受它。唯一不是我在問題中提出的問題:圖像按鈕固定在父級的右側,而不是與文本右對齊。 –

1

試試這一招(未經測試):

  1. 創建一個空的ViewImageButton
  2. 對齊的確切大小這個空ViewparentRightRelativeLayout
  3. 設置你的TextViewtoLeftOfViewalignParentLeft
  4. ImageButton仍然toRightOfTextView

完成!

1

我會創建一個LinearLayout(不是對於整個事情,只是爲了這兩個元素),並使用佈局權重來填充空間。例如:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

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

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

1的佈局權重會導致TextView嘗試填充儘可能多的LinearLayout。但是,由於您將ImageButton設置爲將內容包裝在同一個LinearLayout中,因此TextView只能填充屏幕左側和ImageButton的起始位置之間的空間。

+0

也適用,但也在這裏:圖像按鈕固定在父級的右側,而不是「浮動」到文本視圖的右側。 –

+0

對,這是應該做的。我會盡力讓你成爲一個截圖,但試試看,你會明白我的意思。 – AdamMc331

+0

我試過你的解決方案,它工作正常。但正如我所說,ImageButton始終位於父級的右側,而不是文本視圖的右側,而文本變大時則爲「浮動」。所以你的解決方案並沒有完全解決我的問題。 –

1

你可以試試這個:

你可以用單TextView的,它可以減少你的觀點取代你的TextView和ImageButton的。

怎麼樣?

<TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawableRight="@drawable/ic_launcher" 
     android:gravity="center_vertical" 
     android:text="Your Text OverHere" /> 

希望這有助於你以某種方式......