可变宽度 TextView 旁边的固定宽度 TextView
Fixed width TextView next to variable width TextView
我正在尝试创建一个布局,其中两个 TextViews
彼此相邻。第一个 TextView
具有可变长度并且是单行的。第二个 TextView
包含一个固定标签,应该 而不是 被省略。
第二个标签必须紧挨着第一个标签。
以下是我要完成的两个示例:
这是我到目前为止所取得的成就:
使用此布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/my_bg"
android:padding="16dp"
tools:layout_height="60dp">
<TextView
android:layout_centerVertical="true"
android:id="@+id/tv_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_updated"
android:textSize="17sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="16dp"
tools:text="This label is short"/>
<TextView
android:id="@id/tv_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="@string/updated"
android:textAllCaps="true"
android:textColor="@color/red"
android:textStyle="bold"/>
</RelativeLayout>
剩下的问题是容器的宽度与其父容器相匹配,而它应该是 wrap_content
。
还有其他方法可以解决这个问题吗?
希望对您有所帮助。可能需要稍作调整,但几个隐藏的视图可能会实现您正在寻找的内容。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="16dp"
tools:layout_height="60dp">
<TextView
android:layout_centerVertical="true"
android:id="@+id/tv_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="17sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="16dp"
tools:text="This label is short" />
<TextView
android:layout_centerVertical="true"
android:id="@+id/hidden_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/hidden_updated"
android:layout_alignParentLeft="true"
android:textSize="17sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="16dp"
tools:text="This label is short"
android:visibility="invisible"/>
<TextView
android:id="@+id/tv_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/tv_street"
android:text="updated"
android:textAllCaps="true"
android:textStyle="bold"/>
<TextView
android:id="@+id/hidden_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="updated"
android:textAllCaps="true"
android:textStyle="bold"
android:visibility="invisible"/>
</RelativeLayout>
检查下面的代码,它会对你有所帮助
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="horizontal"
>
<TextView
android:layout_centerVertical="true"
android:id="@+id/tv_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_updated"
android:textSize="17sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_weight="1"
android:layout_marginLeft="16dp"
tools:text="This is fdfdfffddfdfdfffdfdfdfdffshort"/>
<TextView
android:id="@id/tv_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Updated"
android:textAllCaps="true"
android:textStyle="bold"/>
</LinearLayout>
根据您的要求,如果行不通,LinearLayout 是更好的选择,请告诉我
你最好使用线性布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="horizontal"
android:gravity="center_vertical"
tools:layout_height="60dp">
<TextView
android:id="@+id/tv_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="16dp"
tools:text="This label is short"/>
<TextView
android:id="@id/tv_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="UPDATED"
android:textAllCaps="true"
android:textColor="@color/red"
android:textStyle="bold"/>
</LinearLayout>
已编辑:
这里证明
此处发布的解决方案是在布局中不存在其他元素的情况下。
我提出了一个在 RelativeLayout 中使用的解决方案,您可能有固定的结束视图或其他视图。 (用例:购物清单视图中的产品项目)
主要思想是将 textView 包装在 LinearLayout 中,并让线性布局根据其子项改变大小。 TextViews 始终保持并排。将变量 TextView 宽度分配为 0dp,将权重分配为 1(一)。然后将固定的TextView宽度分配给wrap_content.
使 textView 宽度为 0dp 且权重 = 1 会导致它可拉伸并占用所有可用剩余 space。
现在,由于可变文本,LinearLayout 会向任一方向增长,任何方向都可能与其他视图重叠。
但是,如果我们尝试通过 alignEndOf 和 alignStartOf LinearLayout 来固定边界,框架将固定 LinearLayout 在端点之间拉伸的大小。这将导致变量 textView 覆盖所有剩余可用 space,即使内容文本不够大。
这导致,固定的 textView 放置在靠近右端点的地方,在较小内容长度的可变文本和它本身之间有空 space。
因此,建议的解决方案。引入另一种布局来固定边界,让内部线性布局在外部布局限制内自由增长或折叠。
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/leftView"
android:layout_toStartOf="@id/rightView"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/card_name"
style="@style/textStyleCardName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:text="Platinum Card" />
<TextView
android:id="@+id/card_number"
style="@style/textStyleCardNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="none"
android:singleLine="true"
android:text="**** 3308" />
</LinearLayout>
</LinearLayout>
我正在尝试创建一个布局,其中两个 TextViews
彼此相邻。第一个 TextView
具有可变长度并且是单行的。第二个 TextView
包含一个固定标签,应该 而不是 被省略。
第二个标签必须紧挨着第一个标签。
以下是我要完成的两个示例:
这是我到目前为止所取得的成就:
使用此布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/my_bg"
android:padding="16dp"
tools:layout_height="60dp">
<TextView
android:layout_centerVertical="true"
android:id="@+id/tv_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_updated"
android:textSize="17sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="16dp"
tools:text="This label is short"/>
<TextView
android:id="@id/tv_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="@string/updated"
android:textAllCaps="true"
android:textColor="@color/red"
android:textStyle="bold"/>
</RelativeLayout>
剩下的问题是容器的宽度与其父容器相匹配,而它应该是 wrap_content
。
还有其他方法可以解决这个问题吗?
希望对您有所帮助。可能需要稍作调整,但几个隐藏的视图可能会实现您正在寻找的内容。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="16dp"
tools:layout_height="60dp">
<TextView
android:layout_centerVertical="true"
android:id="@+id/tv_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="17sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="16dp"
tools:text="This label is short" />
<TextView
android:layout_centerVertical="true"
android:id="@+id/hidden_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/hidden_updated"
android:layout_alignParentLeft="true"
android:textSize="17sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="16dp"
tools:text="This label is short"
android:visibility="invisible"/>
<TextView
android:id="@+id/tv_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/tv_street"
android:text="updated"
android:textAllCaps="true"
android:textStyle="bold"/>
<TextView
android:id="@+id/hidden_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="updated"
android:textAllCaps="true"
android:textStyle="bold"
android:visibility="invisible"/>
</RelativeLayout>
检查下面的代码,它会对你有所帮助
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="horizontal"
>
<TextView
android:layout_centerVertical="true"
android:id="@+id/tv_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_updated"
android:textSize="17sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_weight="1"
android:layout_marginLeft="16dp"
tools:text="This is fdfdfffddfdfdfffdfdfdfdffshort"/>
<TextView
android:id="@id/tv_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Updated"
android:textAllCaps="true"
android:textStyle="bold"/>
</LinearLayout>
根据您的要求,如果行不通,LinearLayout 是更好的选择,请告诉我
你最好使用线性布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="horizontal"
android:gravity="center_vertical"
tools:layout_height="60dp">
<TextView
android:id="@+id/tv_street"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="16dp"
tools:text="This label is short"/>
<TextView
android:id="@id/tv_updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="UPDATED"
android:textAllCaps="true"
android:textColor="@color/red"
android:textStyle="bold"/>
</LinearLayout>
已编辑: 这里证明
此处发布的解决方案是在布局中不存在其他元素的情况下。
我提出了一个在 RelativeLayout 中使用的解决方案,您可能有固定的结束视图或其他视图。 (用例:购物清单视图中的产品项目)
主要思想是将 textView 包装在 LinearLayout 中,并让线性布局根据其子项改变大小。 TextViews 始终保持并排。将变量 TextView 宽度分配为 0dp,将权重分配为 1(一)。然后将固定的TextView宽度分配给wrap_content.
使 textView 宽度为 0dp 且权重 = 1 会导致它可拉伸并占用所有可用剩余 space。
现在,由于可变文本,LinearLayout 会向任一方向增长,任何方向都可能与其他视图重叠。
但是,如果我们尝试通过 alignEndOf 和 alignStartOf LinearLayout 来固定边界,框架将固定 LinearLayout 在端点之间拉伸的大小。这将导致变量 textView 覆盖所有剩余可用 space,即使内容文本不够大。
这导致,固定的 textView 放置在靠近右端点的地方,在较小内容长度的可变文本和它本身之间有空 space。
因此,建议的解决方案。引入另一种布局来固定边界,让内部线性布局在外部布局限制内自由增长或折叠。
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/leftView"
android:layout_toStartOf="@id/rightView"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/card_name"
style="@style/textStyleCardName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:text="Platinum Card" />
<TextView
android:id="@+id/card_number"
style="@style/textStyleCardNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="none"
android:singleLine="true"
android:text="**** 3308" />
</LinearLayout>
</LinearLayout>