2016-09-26 10 views
1

TextViewsandroid:layout_width="wrap_content"が設定されているヒントが設定されていて、TextViewに入力されたテキストがヒントの幅-textの場合、TextViewはその幅を変更せずにテキストをラップします。ただし、入力されたテキストがヒントテキストの幅よりも広く、TextViewが広がる余地がある場合は、テキストが折り返されます。TextViewでAndroidのテキストの幅に幅を変更させる方法

ヒントテキストの幅(TextViewの元の幅)に関係なく、TextViewがテキストに縮小されるのが理想ですが、私はまた、 テキストの幅をにしてください。その幅のTextViewsetWidthと電話することができます。

初期化後に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を3の整数値に設定すると、EditTextまたはTextViewで少なくとも3文字の幅を確保する必要があります。 maxEmsも設定できます。

私はあなたから理解したことは、ヒント文字の長さの幅のテキストが欲しいということです。ヒントの長さよりも多くのテキストを入力すると、テキストの長さが入力されるまでテキストが縮小されます。そして、それが完了し、入力されたテキストをすべて削除したら、同じヒントの長さの幅のサイズを再度使用する必要があります。

上記の情報が記載されています。

テスト

あなたのXMLレイアウトに追加します。

<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" /> 

は、あなたの活動にこれを追加します。

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

今すぐ実行して確認し、それはあなたが探しているもののために正確に動作しています。

+0

最小または最大幅を設定する方法を質問していません – shoe

+0

ヒントは実際には内容ではなく、ヒントは変更されないため、最小幅を設定しないでくださいヒントに等しい?内容が大きければ、TextViewはテキストサイズに従います。そうでない場合は、ヒントの最小幅を尊重します –

+0

更新された回答を確認し、うまくいかなかった場合は要件を明確にします。 –

関連する問題