android - 将两个 TextView 并排放置在布局中

标签 android textview

我有两个 TextView 需要在布局中并排放置,我必须遵守两条规则:

  • Textview2 需要始终完全显示。

  • 如果布局中没有足够的空间,则必须裁剪 Textview1。

例子:

TextView 1| TextView 2

Teeeeeeeeeeeeeeeeeeeextview1...|textview2

有什么想法吗?

我发现可能可行的唯一方法是使用 textview2 的文本创建一个可绘制对象,并将其作为与 textview1 的 coumpoundDrawable 影响。

最佳答案

将两个 TextView 包装在一个 LinearLayout 中。将布局权重 0 分配给 textview2,将布局权重 1 分配给 textview2。

查看这里了解更多信息:Linear Layout Weight

如果您使用下面的示例,您会看到 LinearLayout 首先将空间分配给 textview2(权重为 0),然后将剩余的空间分配给 textview1(权重为 1)。如果没有足够的空间容纳两个 TextView,则 textview1 将首先被椭圆化。在下面的示例中,只有当 LinearLayout 小于 textview2 本身的大小时,textview2 才会变成椭圆形。为 FrameLayout 分配一个特定的布局宽度,看看会发生什么。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="match_parent" 
    android:background="#FF0000FF">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#FFFF0000"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="textview1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:background="#FF00FF00"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="textview2" />
    </LinearLayout>

</FrameLayout>

关于android - 将两个 TextView 并排放置在布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21778632/

相关文章:

Imageview上的Android自定义listview点击获取textview数据并打开 Activity

Android:使用一般样式应用边距不起作用

android - android中的处理性能

android - setText 后的 TextView 和 Size

ios - UIEdgeInsets.init 不适用于 Xcode 中的 Swift

java - 自定义 ArrayAdapter.add() 不改变我的 TextView

Android-将 TextView 的宽度设置为按钮 1 或按钮 2 的左侧

android - 错误 : Google id_token is not allowed to be used with this application. Firebase 访问在工作后失败

android - 如何在滑动刷新android中刷新布局

java - 无法打开zip文件。 Gradle的依赖项缓存可能已损坏(有时会在网络连接超时后发生。)