2016-11-25 8 views
3

layout_width="wrap_content"의 TextView가 있고 텍스트를 포함하기 위해 두 번째 줄로 줄 바꿈해야하는 경우 여백을 고려하여 사용 가능한 모든 공간을 사용하도록 너비가 조정됩니다. 그런데 왜 끝 부분에 패딩이 있습니까? 방금 에 wrap_content으로 말 했으므로이 내용을 포장해야합니다. 이 버그는 주식 메신저 앱의 채팅 UI에 표시됩니다. (이미지는 내 응용 프로그램에서 가져온 것이지만 추가 공간은 이 아니며 9 패치의이 아님)왜 여러 줄이있을 때 TextView에 끝 패딩이 있습니까?

해결 방법은 무엇입니까?

업데이트 : 응답자/commenters가 요점을 놓쳤습니다. 내 앱에서 스타일을 지정했기 때문에 내가 업로드 한 이미지가 오해의 소지가 있었을 수도 있습니다. 이 문제는 모든 TextView에서 발생하며 뷰 경계가 더 이상 빡빡하지 않게 배경을 스타일링하여 볼 수 있습니다. 다른 이미지를 업로드했습니다. 다음은 이미지의 TextViews에 대한 XML입니다 : 귀하의 게시물을 볼 때 처음에는

 <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="20dp" 
    android:layout_marginStart="20dp" 
    android:background="#dddddd" 
    android:text="This doesn't wrap" 
    android:layout_marginTop="20dp" 
    android:layout_marginBottom="20dp" 
    android:layout_gravity="center_horizontal" 
    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="20dp" 
    android:layout_marginStart="20dp" 
    android:layout_gravity="center_horizontal" 
    android:background="#dddddd" 
    android:text="This wraps and look, the bounds does not fit tight against the right edge of text" 
    /> 

enter image description here

+0

아마도 단어가 맞지 않습니까? 어쩌면 당신의 9 패치가 내부 배경을 가지고 있을까요? – Blackbelt

+0

나는 나의 대답을 업데이 트했다, 그것은 지금 당신의 요점을 커버해야한다 –

답변

1

는, 내가 문제가 몇 가지 기본 패딩이 자신의 기본 스타일에 정의 된 표준 안드로이드 텍스트 뷰가 때문이라고 생각 . 하나는 제거하고자하는 경우, 사람들은 그것을 같은 시도 할 수 있습니다 :

android:paddingEnd="0dp" 

또는

android:paddingRight="0dp" 

게시물이 업데이트되었습니다으로

, 나는 당신의 문제가 패딩에서 제공하지 않음을 이해를하지만, 단어에서 쌈. 실제로 표시 할 줄이 여러 개인 경우 Android TextView는 사용 가능한 전체 공간을 너비로 사용합니다.

in this post과 같이 표준 해결책은 없으므로 텍스트보기를 사용자 정의하여 채우면 너비가 고정되어야합니다.

아래 ("하늘"대답에서 영감) 작업 갈까요처럼 텍스트 뷰의 된 onMeasure 메서드를 오버라이드 (override) :

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int specModeW = MeasureSpec.getMode(widthMeasureSpec); 
    if (specModeW != MeasureSpec.EXACTLY) { 
     Layout layout = getLayout(); 
     int linesCount = layout.getLineCount(); 
     if (linesCount > 1) { 
      float textRealMaxWidth = 0; 
      for (int n = 0; n < linesCount; ++n) { 
       textRealMaxWidth = Math.max(textRealMaxWidth, layout.getLineWidth(n)); 
      } 
      int w = (int) Math.ceil(textRealMaxWidth); 
      if (w < getMeasuredWidth()) { 
       super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST), 
         heightMeasureSpec); 
      } 
     } 
    } 
} 
+1

고마워. 나는 같은 질문을 찾고 있지만 그 사람이 오지 않았다. 확실히 똑같은 문제. 3 년 후 슬프게도, 우리는 여전히이 문제를 해결해야합니다. – user3175580

0

"에 대해"단어 "적합"와 사이에 적합하지 않기 때문입니다 확신 오른쪽 가장자리. 이것을 시험해보고 싶다면, 텍스트를 각각의 사이에 하나의 공백으로 변경하십시오.

0

패딩을 제대로 설명하지는 않았지만 선택된 대답이 도움이되는 것으로 나타났습니다. 선택한 대답을 this post's answer과 결합하여 패딩과 함께 작동하는보기를 만들었습니다. 참고로, 다른 게시물의 답변에는보기가 때때로 약간의 픽셀만큼 끝나기도하는 결함이있었습니다.

public class TightTextView extends TextView { 
    public TightTextView(Context context) { 
    super(context); 
    } 

    public TightTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public TightTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int specModeW = MeasureSpec.getMode(widthMeasureSpec); 
    if (specModeW != MeasureSpec.EXACTLY) { 
     Layout layout = getLayout(); 
     if (layout != null) { 
     int w = (int) Math.ceil(getMaxLineWidth(layout)) + getCompoundPaddingLeft() + getCompoundPaddingRight(); 
     if (w < getMeasuredWidth()) { 
      super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST), 
      heightMeasureSpec); 
     } 
     } 
    } 
    } 

    private float getMaxLineWidth(Layout layout) { 
    float max_width = 0.0f; 
    int lines = layout.getLineCount(); 
    for (int i = 0; i < lines; i++) { 
     if (layout.getLineWidth(i) > max_width) { 
     max_width = layout.getLineWidth(i); 
     } 
    } 
    return max_width; 
    } 
} 
+0

성가신 문제가 무엇인지에 대한 솔루션을 개선하는 데 도움을 주셔서 감사합니다. – user3175580