5

我有一个左对齐的TextView和右对齐的按钮并排。我希望按钮在右边占用尽可能多的空间(取决于其中的文本),左边的文本尽可能多地填充并省略任何溢出。并排显示Android TextView和Button,椭圆左边TextView

|Long title that may or may not ellipsi... <Button with text>| 

我已阅读并尝试了很多似乎有类似问题的其他帖子,其中没有一篇适合我。我已经尝试使用带权重的LinearLayout以及带有layout_toLeftOf赋值的RelativeLayout,其中没有一个导致我需要的。

这是我的LinearLayout代码(带有不必要的部分),我给了左边的TextView layout_weight为1,按钮为layout_weight为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> 

更换与0的TextView的layout_weight实际上允许右侧按钮正确适合在屏幕上完全,但左文仍然没有露面。如果我为TextView和按钮将layout_weights设置为0,然后将TextView的宽度从0dip更改为wrap_content,则会显示所有内容,但该按钮会被挤压以填充剩余空间(并且内部文本被截断)。

这是我尝试用的RelativeLayout:

<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> 

一切都对准罚款和显示出来,除了左TextView的(当它太长)重叠,并且按钮的顶部,而不是截断和ellipsizing出现。不应该android:layout_toLeftOf“@ layout/action_buttons”指定TextView应该停留在按钮的左边界?

我已经试过看似在本网站上可以找到的与此问题有关的所有信息,但我仍然无法获得解决方案。任何帮助将不胜感激!

+0

随着你的' RelativeLayout'尝试,你有没有尝试'android:layout_width =“wrap_content”'以防止'TextView'重叠'Button'时文本很长? – Squonk

+0

是的,对于TextView,android:layout_width =“wrap_content”和android:layout_width =“fill_parent”给出了相同的重叠问题。 – socoho

+0

我一直在eclipse布局编辑器中尝试了大约30分钟,我找不到答案 - 但后来布局并不是我最强烈的观点。如果你还没有在48小时内解决它,请在这里以@MisterSquonk开头添加评论,并且应该提醒我 - 我将在这个问题上加一个奖励,看看它是否吸引了任何布局大师。需要48小时才能有资格获得奖励。 – Squonk

回答

17

这将这样的伎俩为您提供:

<?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> 

这里是它看起来运行时,如: Shorter Button

并再次用更多的文字按钮: Longer Button

+0

非常感谢!这工作:)事实证明,导致我的问题的主要原因是从我使用的自定义按钮。 – socoho

+2

请注意,使用此解决方案时,该按钮始终为右对齐...仍尝试找到解决方案,如果文本很小,右元素向左移动以留在文本旁边 – Thymine