如何使 TextView 只在一行中?
How to make TextView be in only one row?
我有一个列表项,其中包含两个 LinearLayout 中的 TextView,垂直,一个在另一个之上。较高的数据不是静态的,我是从网上获取的,如果它太长,它会开始填充新行并且较低的视图会消失。所以我想让第一个视图只填充一行。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="@dimen/margin_small">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com..view.ContactView
android:id="@+id/contact_icon"
style="@style/ContactListInitialsText"
android:layout_width="@dimen/contact_favorite_icon_size"
android:layout_height="@dimen/contact_favorite_icon_size"
app:request_photo="true"
app:show_contact_presence="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginStart="@dimen/margin_small"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="@color/search_result_text_color"
android:textSize="16sp" />
<TextView
android:id="@+id/contact_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
</LinearLayout>
<CheckBox
android:id="@+id/btn_star"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:button="@drawable/btn_star_select"
android:paddingEnd="@dimen/margin_small" />
</LinearLayout>
</RelativeLayout>
一个不错的解决方案是设置这些属性:
android:ellipsize="end"
android:maxLines="1"
Maxlines = 1,不允许textview展开;
Ellipsize = 在末尾添加 3 个点,表示还有更多隐藏文本。
我有一个列表项,其中包含两个 LinearLayout 中的 TextView,垂直,一个在另一个之上。较高的数据不是静态的,我是从网上获取的,如果它太长,它会开始填充新行并且较低的视图会消失。所以我想让第一个视图只填充一行。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="@dimen/margin_small">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com..view.ContactView
android:id="@+id/contact_icon"
style="@style/ContactListInitialsText"
android:layout_width="@dimen/contact_favorite_icon_size"
android:layout_height="@dimen/contact_favorite_icon_size"
app:request_photo="true"
app:show_contact_presence="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginStart="@dimen/margin_small"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="@color/search_result_text_color"
android:textSize="16sp" />
<TextView
android:id="@+id/contact_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
</LinearLayout>
<CheckBox
android:id="@+id/btn_star"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:button="@drawable/btn_star_select"
android:paddingEnd="@dimen/margin_small" />
</LinearLayout>
</RelativeLayout>
一个不错的解决方案是设置这些属性:
android:ellipsize="end"
android:maxLines="1"
Maxlines = 1,不允许textview展开; Ellipsize = 在末尾添加 3 个点,表示还有更多隐藏文本。