Android TextView 和 Button 并排,椭圆形左侧 TextView

标签 android android-layout android-relativelayout android-linearlayout

我有一个左对齐的 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>

将 TextView 的 layout_weight 替换为 0 实际上可以使右侧按钮完全适合屏幕,但左侧文本仍然不显示。如果我将 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(当它太长时)重叠并出现在按钮的顶部而不是截断和省略。 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>

这是运行时的样子: Shorter Button

还有一个带有更多文本的按钮: Longer Button

关于Android TextView 和 Button 并排,椭圆形左侧 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10822096/

相关文章:

android - TextView 中文本顶部的额外空间?

android - java.util.zip.ZipException : duplicate entry: com/facebook/ads/AbstractAdListener. 类

android截取屏幕外页面

android - 关于如何从 URI 获取 Exif 数据的最终答案

android: Galaxy Note 2:应该为哪个像素布局图像/图标准备?

java - 自定义布局,onMeasure/onLayout不断调用

android - 动态创建 TableRow 然后 RelativeLayout 不显示

Android Studio 找不到默认 Activity

android - 如何更改 Android 微调器文本颜色?

Android:如何在同一布局中将按钮状态链接到 TextView 状态