android - 两个 TextView 并排 - 两个 TextView 都应该尽可能多地占用空间
问题描述
我在一个 LinearLayout 中有两个 TextView,我希望这两个文本都占用 LinearLayout 中给定的空间,但不会相互切断。我试图与之合作,minWidth
但layout_weight
忽略了它。这是一个例子:
<TextView
android:id="@+id/textOrigin"
android:layout_width="0dp"
android:layout_height="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:layout_weight="1"
android:minWidth="170dp". <-- does not work.../>
<TextView
android:id="@+id/textDestination"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:layout_weight="0"
android:gravity="end"/>
问题是第二个 TextView 可能会很长,如果宽度是 wrap_content 会切断对齐的第一个 TextView。所以我想设置一个minWidth。
|<TextView>......<TextView>| --> no cut off normal position
|<TextViewcanbeverylong...><TextView>| --> left one very long. right one should have minWidth of 100 and then ellipsized
|<TextView><TextViewcanbeverylongtoo...>| --> right one very long. left one should have minWidth of 100 and then ellipsized
所有三种情况都应该有效
这与我的问题相似但不一样。上面的 Ticket 只是解决了在一个方向上不切断文本,所以只有当左边的文本很长时,右边的文本才不会被切断,但我希望在两个方向上都这样。
解决方案: 这是解决方案:只需将 Width 更改为 wrap_content。然后可以使用 minWidth。
<TextView
android:id="@+id/textOrigin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:layout_weight="1"
android:minWidth="170dp"/>
<TextView
android:id="@+id/textDestination"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:layout_weight="1"
android:minWidth="170dp"
android:gravity="end"/>
解决方案
试试这个代码,也许它有效!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textOrigin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/textDestination"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:layout_weight="0.5"
android:gravity="end"/>
</LinearLayout>
推荐阅读
- amazon-web-services - 如何在 spark-submit 命令中增加 AM 容器大小?错误:容器运行超出物理内存限制
- java - Spring Boot 无法识别带有发送请求参数的控制器
- javascript - 使用选定的选项填充 html 表
- c - C - 检查字符串的最后一个字符是否等于“#”
- unix - Bourne - 预期二元运算符
- firebase-security - 如何允许 firebase 用户仅访问他们创建的文档
- python - 尝试从 django 应用程序打开 json 文件时出现编码错误
- c++ - C++ 合并重叠字符串
- javascript - 然后将 nodemon 服务器与 MongoDB 连接时出错
- sql - 梅森素数处理