Чтобы воспользоваться принятым ответом пользователя2450263, приведен пример. Как мой комментарий указывает ниже, я использовал атрибут layout_weight, чтобы заставить LinearLayout измерять дважды. Без этой части слово справа будет вытолкнуто с экрана, если основной текст будет эллипсирован.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Using a weight to force the LL to measure twice -->
<TextView
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="@+id/suffix"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Если это не работает для вас, или если вам нужно сделать это в куче разных мест, где вы также можете создать свой собственный виджет.
Вот как вы это сделаете.
Пользовательский вид:
public class EllipsizedTextViewWithSuffix extends LinearLayout {
private TextView mainText, suffix;
public EllipsizedTextViewWithSuffix(Context context) {
this(context, null);
}
public EllipsizedTextViewWithSuffix(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public EllipsizedTextViewWithSuffix(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
inflate(context, R.layout.ellipsized_tv_with_suffix, this);
mainText = (TextView) findViewById(R.id.main_text);
suffix = (TextView) findViewById(R.id.suffix);
}
public void setText(String mainText, String suffix) {
this.mainText.setText(mainText);
this.suffix.setText(suffix);
}
}
Компоновка файл (ellipsized_tv_with_suffix.xml):
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal">
<!-- Using a weight to force the LL to measure twice -->
<TextView
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="@+id/suffix"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>
И затем использовать его вам просто нужно добавить его в другую раскладку.
Замените, пожалуйста, правильное название пакета.
<your.package.name.EllipsizedTextViewWithSuffix
android:id="@+id/ellipsized_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
почему бы не использовать два TextViews – user2450263
динамический текст, вы имеете в виду с этим я пришел с переменной в коде или что? –
Динамический смысл текста, я не знаю этого слова заранее. Он исходит от серверного сервера. – SlowAndSteady