사용자 정의 listView 내부의 textView에 대해 타원 크기가 작동하지 않습니다.

StackOverflow https://stackoverflow.com/questions/1424276

  •  07-07-2019
  •  | 
  •  

문제

아래 XML 레이아웃으로 정의된 사용자 정의 개체가 있는 listView가 있습니다.ID가 "info"인 textView를 한 줄에 줄임표로 표시하고 싶습니다. 속성을 사용해 보았습니다.

android:singleLine="true"
android:ellipsize="end"

성공없이.

예를 들어 레이아웃 너비를 고정 너비로 ​​설정하면 다음과 같습니다.

android:layout_width="100px"

텍스트가 잘 잘립니다.그러나 이식성의 이유로 이는 허용 가능한 솔루션이 아닙니다.

문제를 발견할 수 있나요?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5px"
>
<TextView  
android:id="@+id/destination"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:textSize="22dp"
android:paddingLeft="5px"
/>

<TextView  
android:id="@+id/date"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:textSize="15dp"
android:paddingLeft="5px"
/>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/info_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5px"
android:paddingTop="10px" 
>
    <TableRow>
        <TextView
            android:id="@+id/driver_label"
            android:gravity="right"
            android:paddingRight="5px"
            android:text="@string/driver_label" />
        <TextView
            android:id="@+id/driver" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/passenger_label"
            android:gravity="right"
            android:paddingRight="5px"
            android:text="@string/passenger_label" />
        <TextView
            android:id="@+id/passengers" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/info_label"
            android:gravity="right"
            android:paddingRight="5px" 
            android:text="@string/info_label"/>
        <TextView
            android:id="@+id/info"
            android:layout_width="fill_parent"
            android:singleLine="true"
            android:ellipsize="end" />
    </TableRow>
</TableLayout> 

도움이 되었습니까?

해결책

타원 크기가 깨졌습니다(이동 버그 리포트에 투표하세요, 특히 재현이 불가능하다고 주장하기 때문에) 사소한 해킹을 사용해야 합니다.사용:

android:inputType="text"
android:maxLines="1"

줄임표로 만들고 싶은 모든 것에.또한 사용하지 마십시오. singleLine, 더 이상 사용되지 않습니다.

업데이트:

자세히 살펴보면 테이블이 화면 오른쪽 밖으로 확장되어 있다는 문제가 있습니다.당신의 변경 TableLayout 정의:

<TableLayout
    android:id="@+id/info_table"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="5px"
    android:paddingTop="10px"
    android:shrinkColumns="1">

그 문제를 해결해야 합니다. 그런 다음 위에서 말한 내용을 수행하여 TextView.

다른 팁

더 이상 사용되지 않는 속성을 사용하지 않으면 다음 줄이 나에게 효과적입니다.

android:ellipsize="end" 
android:lines="1"
android:scrollHorizontally="true"

심판 http://code.google.com/p/android/issues/detail?id=882#c9

나는 Steve와 같은 결과를 얻었습니다.

Android : Ellipsize = "marquee"Android : scrollhorizontally = "true"Android : lines = "1"

완벽하게 작동합니다

노력하다...

즉.

android:ellipsize="marquee"
android:focusable="true" android:marqueeRepeatLimit="num|marquee_forever"
android:lines="1" android:focusableInTouchMode="true"
android:scrollHorizontally="true"

선택 윤곽은 주석 #2에 따라 보기가 선택되거나 초점이 맞춰진 경우에만 작동하므로 http://code.google.com/p/android/issues/detail?id=5364

이 3가지 문제(닫혔다가 다시 열림)와 이전 게시물을 확인한 후 핵심은 TextView를 외부 LinearLayout(또는 사용 중인 다른 레이아웃)으로 마무리하고 "레이아웃_너비" 외부 레이아웃 너비가 약간 더 작게 TextView에 포함된 텍스트 길이보다해야 할 것 같습니다 한계 "android:ellipsize"가 올바르게 작동하도록 하기 위한 너비입니다.

팁을 따르세요. 이 문제, 해킹을 위해 이러한 모든 속성을 추가한 후 외부 LinearLayout의 레이아웃_폭도 240dp로 설정했습니다. 예를 들면 다음과 같습니다.

<Linearlayout
  android:layout_width="240dp"
  android:layout_height="wrap_content"
  ...>

    <TextView
     ....
     android:singleLine="true"
     android:lines="1"
     android:maxLines="1"
     android:scrollHorizontally="true"
     android:ellipsize="end"
     android:text="this is a text which length should be longer than 240dp"
    />
</LinearLayout>

"..."을 표시하는 TextView를 성공적으로 끝낼 수는 있지만 그다지 수용 가능하지는 않지만 작동합니다!그리고 일반 TextView에서 사용하든 목록 어댑터의 TextView에서 사용하든 작동한다고 생각합니다.

결과에 대한 스크린샷은 다음과 같습니다!

hope it helps ^^

해당 3가지 문제에 대한 참조:

문제 882

문제 10554

문제 31229

이 속성을 어딘가에서 찾았습니다.

<TextView ...
android:singleLine="true"
android:ellipsize="marquee" />

제겐 잘 작동해요(안드로이드 1.5)

나에게는 이러한 속성이 효과가 있었습니다.API 8 이상에서 테스트했습니다.

XML에서

<TextView
 ....
 android:maxLines="1"
 android:scrollHorizontally="true"
 android:ellipsize="end"
 ....
/>

프로그래밍 방식으로

setMaxLines(1);
setEllipsize(TruncateAt.END);
setHorizontallyScrolling(true);

생성한 사용자 정의 ViewGroup 안에 TextView를 넣은 경우, 측정 후 치수를 변경했다면 반드시 다시 측정하세요.

예를 들어 onLayout에서 새로운 차원을 계산했습니다.child.layout을 호출하기 전에 다음을 호출하세요.

measureChild(child,
    View.MeasureSpec.makeMeasureSpec(childWidth, View.MeasureSpec.EXACTLY),
    View.MeasureSpec.makeMeasureSpec(childHeight, View.MeasureSpec.EXACTLY));

재활용자 보기 카드에서도 동일한 문제가 발생했습니다.다음 코드를 추가하여 이 문제를 해결했습니다.

    <TextView
        android:ellipsize="end"
        android:maxLines="1"/>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top