2015-04-17 41 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" /> 

希望这有助于你以某种方式......