Android TextView在一行中
下面是我的代码。我需要在这个狙击集上获得这样的结果: 当是短字符串时:Android TextView在一行中,android,textview,Android,Textview,下面是我的代码。我需要在这个狙击集上获得这样的结果: 当是短字符串时: 1 line: ABC (123) 当是长字符串时: 1 line: ABC ABC ABC ABC ABC...(123) 有什么帮助吗 <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent">
1 line: ABC (123)
当是长字符串时:
1 line: ABC ABC ABC ABC ABC...(123)
有什么帮助吗
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="1"
android:visibility="visible"
android:text="ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC"
android:textSize="@dimen/text_size_form"/>
<TextView
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:ellipsize="end"
android:maxLines="1"
android:text="123"
android:textSize="@dimen/text_size_form"/>
</LinearLayout>
在文本视图中添加此属性android:singleLine=“true”如果字符串长度超过某个特定长度,则可以检查字符串长度,然后根据需要显示
你想要的最大长度是20
if (str.length()>20) {
str = str.substring(0, 20)+"...";
}
使用下面的代码
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/id1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="1"
android:visibility="visible"
android:text="ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC"
android:textSize="@dimen/text_size_form"/>
<TextView
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:ellipsize="end"
android:maxLines="1"
android:text="123"
android:layout_marginLeft="-3dp"
android:textSize="@dimen/text_size_form"/>
</LinearLayout>
您还可以使用RelativeLayout和属性,如下所示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/id1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/id2"
android:ellipsize="end"
android:singleLine="true"
android:text="ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC"
android:textStyle="bold"
android:visibility="visible" />
<TextView
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ellipsize="end"
android:maxLines="1"
android:text="123"
android:visibility="visible" />
</RelativeLayout>
输出如下图所示
试试-
只需将其添加到xml中
android:ellipsize="middle"
android:singleLine="true"
你有什么问题?我需要保持一行长短。但是当前第二个参数不可见,它是覆盖购买第一个参数这可能会有帮助:好的,但是第二个参数不可见您能提供关于第二个参数的详细信息吗?如何显示?我需要将短字符串ABC(parametr:id2)向左对齐,但第二个参数不可见。请将字符串中的第二个参数指定为
str=str.substring(0,20)+“…”+第二个参数str代码>正常,但第二个参数不可见请仔细检查。它起作用了。注意这些代码:android:layout\u width=“0dp”android:layout\u height=“wrap\u content”android:layout\u weight=“1”可以,如何向左对齐,以获得更好的用户体验?第二个是android:id=“@+id/id2”您知道字符串何时短,何时长吗?如果是,我将更新我的答案。
android:ellipsize="middle"
android:singleLine="true"