Android文本视图和按钮并排,省略左侧文本视图

Android文本视图和按钮并排,省略左侧文本视图,android,android-layout,android-relativelayout,android-linearlayout,Android,Android Layout,Android Relativelayout,Android Linearlayout,我有一个左对齐的文本视图和一个并排右对齐的按钮。我希望按钮在右边(取决于其中的文本)占据尽可能多的空间,左边的文本尽可能多地填充,并在溢出时省略 |Long title that may or may not ellipsi... <Button with text>| |可能省略也可能不省略的长标题| 我读过并尝试过很多其他的帖子,这些帖子似乎都有类似的问题,但没有一篇对我有效。我尝试过使用带有权重的线性布局和带有布局_toLeftOf分配的相对布局,但没有一个是我所需要的 这

我有一个左对齐的文本视图和一个并排右对齐的按钮。我希望按钮在右边(取决于其中的文本)占据尽可能多的空间,左边的文本尽可能多地填充,并在溢出时省略

|Long title that may or may not ellipsi... <Button with text>|
|可能省略也可能不省略的长标题|
我读过并尝试过很多其他的帖子,这些帖子似乎都有类似的问题,但没有一篇对我有效。我尝试过使用带有权重的线性布局和带有布局_toLeftOf分配的相对布局,但没有一个是我所需要的

这是我的LinearLayout代码(去掉了不必要的部分),其中我给左文本视图的布局权重为1,给按钮的布局权重为0。这应该为右侧按钮提供所需的所有空间,并为TextView提供其余空间,但相反,左侧标题停止显示,右侧按钮被弄脏并切断。我已经尝试将文本和按钮的宽度替换为0dip,正如我所看到的建议,这不会改变任何东西

<LinearLayout
    android:layout_height="@dimen/title_bar_height"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:ellipsize="end"
      android:layout_gravity="left|center_vertical"
      android:gravity="left|center_vertical"
      android:textSize="22sp"
      android:lines="1"/>
  <include layout="@layout/action_buttons"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="0"/>
</LinearLayout>

将TextView的布局权重替换为0实际上允许右侧按钮完全适合屏幕,但左侧文本仍然不会显示。如果我将TextView和按钮的布局权重都设置为0,然后我将TextView的宽度从0dip更改为wrap\u内容,所有内容都会显示,但按钮会被压扁以填充剩余空间(并且内部的文本会被截断)

以下是我对相对论的尝试:

<RelativeLayout
    android:layout_height="@dimen/title_bar_height"
    android:layout_width="wrap_content">
  <include layout="@layout/action_buttons"/>
  <TextView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_gravity="center_vertical"
      android:layout_alignParentLeft="true"
      android:layout_toLeftOf="@layout/action_buttons"
      android:gravity="center_vertical"
      android:scaleType="center"
      android:textSize="22sp"
      android:ellipsize="end"
      android:lines="1"/>
</RelativeLayout>

所有内容都很好地对齐并显示出来,除了左侧文本视图(当它太长时)重叠并显示在按钮顶部,而不是截断和省略。android:layout_toLeftOf“@layout/action_buttons”是否应该指定TextView应该保持在按钮的左边界


我已经尝试了在这个网站上找到的与这个问题相关的一切,但我仍然找不到解决方案。任何帮助都将不胜感激

这将为您带来好处:

<?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" >
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="left|center_vertical"
        android:singleLine="true"
        android:text="Some really long textttttttttt tooooooooooo make the ellipsize work in the preview"
        android:textSize="22sp" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button Text" />
</LinearLayout>

以下是跑步时的样子:

再次使用包含更多文本的按钮:

通过您的
RelativeLayout
尝试,您是否尝试过
android:layout\u width=“wrap\u content”
来防止
TextView
在文本较长时重叠
按钮?是的,android:layout\u width=“wrap\u content”和android:layout\u width=“fill\u parent”对于TextView,我给出了同样的重叠问题。我已经在eclipse布局编辑器中尝试了大约30分钟,但我找不到答案,但布局并不是我的强项。如果你在48小时内还没有解决它,请在这里添加一条以@MisterSquonk开头的评论,它应该会提醒我-我会悬赏这个问题,看看它是否吸引了任何布局大师。不过,一个问题需要48小时才能获得赏金。@Connie,看我下面的答案,我相信它符合你的要求。如果不是你需要的,请告诉我。你可以用这个。相对论和负边际非常感谢你!这是有效的:)事实证明,导致我的问题的主要原因是我使用的自定义按钮。注意,使用此解决方案时,按钮总是右对齐。。。仍在试图找到一种解决方案,其中,如果文本已被删除,右元素将向左移动以停留在文本旁边small@Thymine,你做到了吗?你介意分享一下你是怎么做的吗?