2016-09-26 4 views
1

TextViews에세트의 힌트가 설정되어 있고 TextView에 입력 된 텍스트의 너비가 너비만큼 넓지 않은 경우 -text이면 TextView은 텍스트를 감싸기 위해 너비를 변경하지 않습니다. 그러나 입력 된 텍스트가 힌트 텍스트의 너비보다 넓고 TextView의 넓이가있는 경우 텍스트가 줄 바꿈됩니다.TextView에서 너비를 Android에서 텍스트의 너비로 변경하는 방법

이상적으로는 TextView이 힌트 텍스트의 너비 (원래 너비는 TextView)에 관계없이 내 텍스트로 축소되는 것이 가장 이상적이지만, 내 텍스트의 너비는 이니, 그 너비 인 TextView에서 setWidth으로 전화하면됩니다.

이미 초기화 후 TextView의 최소 너비를 0으로 변경하려고 시도했지만 문제가 해결되지 않습니다. 재미있는 점은 힌트 텍스트보다 더 넓은 텍스트를 입력하면 텍스트에서 문자를 가져 와서 힌트 텍스트가 가져야하는 너비가 될 때까지 축소됩니다. . 내가 원했던 것은 힌트 텍스트의 너비에 관계없이 항상 텍스트의 너비로 축소되는 것입니다.

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center"> 
     <TextView 
      android:id="@+id/month_textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:hint="mm" 
      android:textColorHint="@color/hint_color" 
      android:singleLine="true" 
      android:textSize="40sp" 
      android:maxLength="2"/> 
     <TextView 
      android:id="@+id/first_hypen_textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:hint="-" 
      android:singleLine="true" 
      android:textSize="40sp"/> 
     <TextView 
      android:id="@+id/day_textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:hint="dd" 
      android:textColorHint="@color/hint_color" 
      android:singleLine="true" 
      android:textSize="40sp" 
      android:maxLength="2"/> 
     <TextView 
      android:id="@+id/second_hyphen_textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:hint="-" 
      android:singleLine="true" 
      android:textSize="40sp"/> 
     <TextView 
      android:id="@+id/year_textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:hint="yyyy" 
      android:textColorHint="@color/hint_color" 
      android:singleLine="true" 
      android:textSize="40sp" 
      android:maxLength="4"/> 
    </LinearLayout> 
+0

TextView에 'android : singleLine = "true"'를 추가하십시오. 도움이 되나요? – Vikrant

+0

@Vikrant는 이미 그걸 가지고 있습니다. – shoe

+0

전체 XML 레이아웃을 표시 – user1506104

답변

1

나는 결국 텍스트가 들어있을 때 TextView에서 ""으로 힌트를 설정 한 다음 힌트를 원래의 힌트로 다시 설정합니다. 모든 텍스트가 삭제되면 힌트가 다시 설정됩니다.이렇게하면 텍스트가 입력 된 후에 TextView이 최소 힌트 텍스트 폭으로 인해 강제로 적용되는 모든 설정이 새 힌트가 설정된 후 다시 구성되고 새 최소 TextView 너비가 0으로 설정된 후 다시 구성됩니다 새로운 힌트 - 텍스트 너비 ("")로 인해; 모든 텍스트가 삭제되면 힌트는 여전히 예상대로 표시됩니다.

1

사용 "EMS"힌트 길이

예와 : 여기

내 레이아웃입니다 "

TextView tvDay = (TextView) findViewById(R.id.day_textview); 
tvDay.setEms(tvDay.getHint().length()); 

그래서 사용자 지정 코드에서, EMS ="2 "

을 OR : XML 디자인에서 당신이 day_textview 위해 여기

android:ems="2" 

는 길이가 2 다음 안드로이드입니다입니다, 힌트"DD "가 ems "는 일반적으로"M "인 가장 넓은 문자의 크기를 나타냅니다. minEms를 정수 값으로 설정하면 EditText 또는 TextView에서 최소 3 자 이상이어야합니다. maxEms도 설정할 수 있습니다.

내가 당신에게서 이해 한 것은 힌트 문자 길이 너비가있는 텍스트를 원한다는 것입니다. 힌트 길이보다 많은 텍스트를 입력하면 텍스트 길이가 입력 될 때까지 텍스트가 축소됩니다. 그리고 일단 그것이 끝나면 입력 된 모든 텍스트를 제거한 다음 다시 동일한 길이의 너비를 가져야합니다.

상기 정보는 제공됩니다.

테스트 예 :

EditText edt = (EditText) findViewById(R.id.edtCheck); 
     edt.setEms(3);//3 just for test 
     edt.setMaxEms(1000);//1000 just for test 

지금 실행하고 검사가 당신이 찾고있는 무슨에 대한 정확한 작동입니다 :

<EditText 
     android:id="@+id/edtCheck" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:hint="abc" 
     android:maxLines="1" 
     android:text="I am an Android Developer" /> 

은 활동이 추가 :

는 XML 레이아웃에 추가합니다.

+0

최소 또는 최대 너비를 설정하는 방법을 묻지 않는 질문 – shoe

+0

힌트는 실제로 내용이 아니며 힌트가 변경되지 않아야하므로 왜 최소 너비를 설정하지 않는가? 힌트와 같을까요? 내용이 더 크면 TextView가 텍스트 크기를 따릅니다. 그렇지 않으면 힌트의 최소 너비를 고려합니다. –

+0

업데이트 된 답변 확인, 작동하지 않으면 요구 사항이 명확 해집니다. –

관련 문제