Android 如何预防;包装“U内容”;文本视图是否会将另一个视图推离视线?
布局中有两个文本视图。我想这样将它们相邻显示: |[文本视图1][文本视图2]|Android 如何预防;包装“U内容”;文本视图是否会将另一个视图推离视线?,android,Android,布局中有两个文本视图。我想这样将它们相邻显示: |[文本视图1][文本视图2]| <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_widt
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"/>
<TextView
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
第一个文本视图的宽度可能会有很大的变化。从几个符号到100个符号,但是如果它太长并且TextView2应该始终可见,我想将其省略。像这样:
|[TextView1TextView1Tex…][TextView2]|
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"/>
<TextView
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
我尝试了以下布局,但它始终在屏幕的末尾显示TextView2:
[文本视图1][文本视图2]|
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"/>
<TextView
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
我曾尝试将TextView1的宽度从0更改为wrap_内容,但随后它将TextView2从视线中推出,如下所示:
view.setMaxWidth((int)(((View)view.getParent()).getWidth() * 0.8));
|[TextView1TextView1TextView1Text…]|
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"/>
<TextView
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
任何帮助都将不胜感激。这可能有些过分,但是
表格布局
起到了作用
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/firstTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"/>
<TextView
android:id="@+id/secondTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"/>
</TableRow>
</TableLayout>
android:shrinkColumns=“0”
是这里的重点。它告诉表只收缩第一列,而保持其他列不变。结果是,系统只会调整第一列的大小,即第一个文本视图。当您仅为其中一个子元素指定权重时,您会告诉android该视图是唯一您关心其可见性的视图。但是,如果您为两个子视图指定权重,则可确保两者都可见。
试试这个解决方案,它适合我,
![在此处输入图像描述][1]
输出:检查下面的屏幕截图:
对这个解决方案并不感到自豪,但它确实做到了, 我已将TextView1和TextView2的宽度设置为“包装内容” 然后在代码中,我将TextView1的maxWidth设置为其父级的80%,如下所示:
view.setMaxWidth((int)(((View)view.getParent()).getWidth() * 0.8));
尝试使用布局权重。将文本视图和布局权重都设置为1。谢谢。我希望文本视图更加灵活。如果文本视图1较短,则尽可能靠近文本视图1,如果文本视图1过长,则移动到屏幕的最末端。删除第二个文本视图的左边距,并不断改变权重。例如,将第二个文本视图的权重设置为0.25谢谢。不幸的是,您的解决方案将屏幕一分为二,这意味着如果TextView1太长,我无法将TextView2靠近边缘。谢谢您的建议。我没有提到我有多行,这使它更难,因为TextView1在某些行上可能很长,而在其他行上可能很短。哇,终于找到了正确的答案。什么都试过了,都没用。谢谢