ImageButton右边是TextView

时间:2015-04-17 17:38:46

标签: android user-interface

我有一个RelativeLayout和两个元素:TextView和ImageButton。 ImageButton应该位于TextView的右侧,所以我使用android:layout_toRightOf属性。 这两个元素都使用android:layout_width =" wrap_content"因为他们的宽度。

现在我的问题:只有当TextView中的文本达到屏幕的整个宽度时,它才会换行到下一行。但此时,ImageButton已经隐藏在屏幕的右侧(因为由于wrap_content,TextView使用父级的全宽)。

enter image description here

所以我想做的是:ImageButton总是在TextView的右边,但是它有一个固定的大小(比方说100dp),这个大小需要为ImageButton保留,所以文本需要在ImageButton"触及"之后立即换行。屏幕的右侧。

我已经尝试过maxWidth和maxEms,但似乎没有按预期工作(它总是那么大,不仅仅是当文本达到最大宽度时)

希望你知道我的意思,否则我会发布代码和截图!

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

尝试这个技巧(未经测试):

  1. 使用View
  2. 的确切大小创建一个空的ImageButton
  3. View
  4. 中的空RelativeLayout parentRight 对齐
  5. TextView 设为LeftOf 设为空View以及 alignParentLeft
  6. ImageButton toRightOf TextView
  7. 完成!

答案 2 :(得分: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开头之间的空间。

答案 3 :(得分: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" />

希望这能以某种方式帮助你......