Android, jak ograniczyć szerokość TextView (i dodać trzy kropki na końcu tekstu)?
Mam TextView
, że chcę ograniczyć znaki tego. Właściwie mogę to zrobić, ale to, czego szukam, to jak dodać trzy kropki (...) na końcu struny. Ten pokazuje, że tekst jest kontynuowany. To jest mój XML, ale nie ma kropek, chociaż ogranicza mój tekst.
<TextView
android:id = "@+id/tvFixture"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_toLeftOf = "@id/ivFixture_Guest"
android:text = "@string/test_06"
android:lines = "1"
android:ems = "3"
android:gravity = "right"
style = "@style/simpletopic.black"
android:ellipsize="end"/>
18 answers
Deprecated:
Dodaj jeszcze jedną właściwość android:singleLine="true"
w widoku tekstu
Aktualizacja:
android:ellipsize="end"
android:maxLines="1"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-21 12:57:59
Oto, czego się nauczyłem, bawiąc się różnymi opcjami zmuszania TextView
do jednej linii (z trzema kropkami i bez nich).
Android: maxLines= " 1 "
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="one two three four five six seven eight nine ten" />
To tylko zmusza tekst do jednej linijki. Każdy dodatkowy tekst jest ukryty.
Powiązane:
- android: maxLines
- Android:singleLine (Uwaga to i to )
- android: linie
Ellipsize= " end "
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:text="one two three four five six seven eight nine ten" />
To odcina tekst, który nie pasuje, ale pozwala użytkownikom wiedzieć, że tekst został obcięty przez dodanie wielokropka (trzy kropki).
Powiązane:
- ellipsize = "start" (...aaabbbccc)
- ellipsize = "middle" (aaa...ccc)
- android: elipsy, znaczenie opcji
Ellipsize= "marquee"
<TextView
android:id="@+id/MarqueeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="one two three four five six seven eight nine ten" />
To sprawia, że tekst przewija się automatycznie po widoku tekstu. Zauważ, że czasami musi być ustawiony w kodzie:
textView.setSelected(true);
Podobno android:maxLines="1"
i android:singleLine="true"
powinny robić w zasadzie to samo, a ponieważ singleLine jest najwyraźniej przestarzała wolałbym jej nie używać, ale kiedy ją wyjmę, markiza nie przewija się już. Wyjęcie maxLines
nie ma na to wpływu.
Powiązane:
HorizontalScrollView with scrollHorizontally
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="one two three four five six seven eight nine ten" />
</HorizontalScrollView>
Pozwala to użytkownikowi na ręczne przewijanie, aby zobaczyć cały wiersz tekstu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:26:36
Wypróbuj tę właściwość TextView w pliku układu..
android:ellipsize="end"
android:maxLines="1"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-15 05:11:18
Rozumiem, że chcesz ograniczyć szerokość do jednej linii, a nie ograniczać jej znakami? Ponieważ singleLine
jest przestarzałe, możesz spróbować użyć następujących razem:
android:maxLines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-06 10:46:55
Np. możesz użyć
android:maxLength="13"
To ograniczy długość texview do 13, ale problem jest, jeśli spróbujesz dodać 3 kropki(...), nie wyświetli go, ponieważ będzie częścią długości textview.
String userName;
if (data.length() >= 13) {
userName = data.substring(0, 13)+ "...";
} else {
userName = data;
}
textView.setText(userName);
Oprócz tego musisz użyć
android:maxLines="1"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-16 06:24:42
Uzyskałem pożądany rezultat używając
android:maxLines="2"
android:minLines="2"
android:ellipsize="end"
Sztuczka jest ustawiona na tę samą wartość maxLines i minLines... i nie tylko android: lines = "2", nie rób sztuczki. Ponadto unikasz wszelkich przestarzałych atrybutów.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-10 07:33:17
Kod:
TextView your_text_view = (TextView) findViewById(R.id.your_id_textview);
your_text_view.setEllipsize(TextUtils.TruncateAt.END);
Xml:
android:maxLines = "5"
Np.
[2]} W Mateusza 13, uczniowie pytali Jezusa, dlaczego mówił do tłumów w przypowieściach. Odpowiedział: "zostało wam dane poznać tajemnice królestwa niebieskiego, ale im nie zostało dane.Wyjście: W Mateusza 13, uczniowie pytali Jezusa, dlaczego mówił do tłumów w przypowieściach. Odpowiedział: "zostało wam dane wiedzieć...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-04 01:36:39
Możesz ograniczyć liczbę znaków textview i dodać (...) po tekście. Załóżmy, że musisz pokazać tylko 5 liter, a następnie musisz pokazać (...), Wystarczy wykonać następujące czynności:
String YourString = "abcdefghijk";
if(YourString.length()>5){
YourString = YourString.substring(0,4)+"...";
your_text_view.setText(YourString);
}else{
your_text_view.setText(YourString); //Dont do any change
}
Mały hack ^_^. Chociaż to nie jest dobre rozwiązanie. Ale praca, która mi się sprawdziła: D
EDIT: Dodałem sprawdzić dla mniej charakter jak na Twój ograniczony nie. postaci.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-01 09:17:38
Używam Horizonal Recyclerview.
1) Tutaj w CardView, TextView jest zniekształcony pionowo podczas używania
android:ellipsize="end"
android:maxLines="1"
Sprawdź pogrubiony tekst grupy Wyman, Jaskolski...
2) ale kiedy użyłem singleLine wraz z ellipsize -
android:ellipsize="end"
android:singleLine="true"
Sprawdź pogrubiony tekst grupy Wyman, Jaskolski...
Drugie rozwiązanie działało dla mnie poprawnie (przy użyciu singleLine). Również przetestowałem w wersji OS: 4.1 i powyżej (do 8.0), działa dobrze bez żadnych awarii.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-18 11:15:32
Musisz dodać następujące linie do układu textview
android:maxLines="1"
android:ellipsize="end"
android:singleLine="true"
Mam nadzieję, że to zadziała.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-01 09:22:24
Aby pracować z atrybutem Android:ellipsize , musisz ograniczyć Szerokość układu widoku TextView, tak aby tekst był poza granicami widoku TextView.
Więc android:layout_width atrybut odgrywa tutaj kluczową rolę, ustaw go odpowiednio.
Jeden przykład może być:
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:text="This is a very long text to be displayed"
android:textSize="12sp"
android:maxLines="1"
/>
Teraz, Jeśli tekst W android: text= "To jest bardzo długi tekst do wyświetlenia" wychodzi z widoku z TextView with a android: layout_width="120dp", android: ellipsize = "end" obetnie tekst i umieści ...(3 kropki) po nim. tj. to jest bardzo długie... zostanie wyświetlony w widoku tekstowym.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-05 09:31:40
Myślę, że podajesz poprawną wysokość i szerokość widoku tekstu. Wtedy twoje rozwiązanie zadziała.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-25 05:21:49
Możesz napisać tę linię w xml, gdzie bierzesz textview
:
android:singleLine="true"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-19 16:22:28
Dodaj te dwie linijki w tekście
android:ellipsize="end"
android:singleLine="true"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-07 10:11:35
Podejście @ AzharShaikh działa dobrze.
android:ellipsize="end"
android:maxLines="1"
Ale zdaję sobie sprawę z problemu, że TextView będzie obcinany przez Worda (domyślnie). Pokaż jeśli mamy tekst w stylu:
test long_line_without_any_space_abcdefgh
Wyświetlony zostanie Widok tekstu:
test...
I znalazłem rozwiązanie, aby poradzić sobie z tym problemem, zastąpić spacje znakiem Unicode no-break, to sprawia, że TextView zawija znaki zamiast słowa:
yourString.replace(" ", "\u00A0");
Wynik:
test long_line_without_any_space_abc...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-07 10:15:44
You just change
Android:layout_width= "wrap_content"
Użyj poniższego wiersza
Android: layout_width= "match_parent"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/visitBox"
android:orientation="vertical" >
<TextView
android:id="@+id/txvrequestTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Abcdefghighiklmnon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/orange_color" />
</LinearLayout>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-15 13:53:05
Możesz to zrobić przez xml
:
<TextView
android:id="@+id/textview"
android:maxLines="1" // or any number of lines you want
android:ellipsize="end"
/>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-06 21:37:34
Użyj
android:singleLine="true"
android:maxLines="1"
app:layout_constrainedWidth="true"
Tak wygląda mój pełny TextView
:
<TextView
android:id="@+id/message_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:maxLines="1"
android:singleLine="true"
android:text="NAME PLACEHOLDER MORE Text"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@id/message_check_sign"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="@id/img_chat_contact"
app:layout_constraintTop_toTopOf="@id/img_chat_contact" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-29 23:36:02