2014-04-24 2 views
1

가능한 한 많은 텍스트를 위젯의 한 TextView에 집어 넣으려는 것이므로 특정 TextView의 텍스트 내용을 하이픈으로 표시하려고합니다. TextView의 크기를 가져 오는 가장 쉬운 방법은 디스플레이 크기로 전체 위젯을 부 풀리는 것입니다. 그런 다음 TextView의 텍스트 내용을 늘리고 줄 수가 변경되면 하이픈 ('-')을 어디에 넣을 지 지켜 볼 것입니다.위젯 내 TextView의 크기

이 오프 스크린 측정은 작동하지만 실제로는 위젯에서 그려지는 것보다 이전에 새 줄을 발견하기 때문에 일부 치수를 잘못 설정 한 것 같습니다. 즉 실제 위젯이 더 넓어 보이는 것 같습니다. 내가 스크린 밖에서 재현 한 것보다.

내가 뭘 잘못하고 있니? 아니면 내가 원하는 것을 얻기위한 또 다른 방법이 있습니까? 하지 전체, kal_medium.xml :

1) 오프 스크린 텍스트 측정

String adaptText(String text) { 
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) 
    .getDefaultDisplay(); 

    // I'm aware these methods are deprecated, but the results are identical 
    // when I'm using the new method: display.getSize(point) 
    int displayWidth = display.getWidth(); 
    int displayHeight = display.getHeight(); 

    LinearLayout root = new LinearLayout(this); 
    LinearLayout sampleLayout = (LinearLayout) LinearLayout.inflate(this, 
     R.layout.kal_medium, root); 
    sampleLayout.measure(displayWidth, displayHeight);   
    sampleLayout.layout(0, 0, displayWidth, displayHeight); 

    TextView saintsView = (TextView) sampleLayout.findViewById(R.id.saints); 
    StringBuilder textBuilder = new StringBuilder(); // result holder 

    int numLines = 1; // there's always one line at least 
    int len = text.length(); 

    for (int i = 0; i < len; i++) { 
    saintsView.setText(textBuilder); 
    if (saintsView.getLineCount() > numLines) {  
     System.out.println("=========BROKEN AT:" + textBuilder); 

     // here goes the code that inserts the hyphen 

     numLines++; 
    } 
    textBuilder.append(text.charAt(i)); 
    } 
    return textBuilder.toString(); 
} 

2) 위젯 레이아웃 파일 :

는 여기에 몇 가지 코드입니다.

<ImageView android:id="@+id/img_bg" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:src="@drawable/bg_post"/> 

... 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@id/date" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="12dp" 
    android:layout_marginTop="2dp" 
    android:padding="0dp"> 

    ...    

    <TextView android:id="@+id/saints" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:singleLine="false" 
     android:ellipsize="none" 
     android:textSize="19dp" 
     android:textStyle="bold" 
     android:gravity="center_vertical|left" 
     android:paddingTop="0dp" 
     android:layout_marginTop="-1dp" 
     android:paddingBottom="20dp"/> 
</LinearLayout> 

답변

0

좋아, 그래서 옆으로이 문제를 떠난, 그리고 내가 해결책을 찾을 재 방문 할 때 : 질문에 제안

내 실제 아이디어를 작동, 단지 거기 하이픈의 삽입 알고리즘의 버그. 하이픈을 삽입하면 라인에 포함될 내용을 정확히 알 수 있습니다.