2017-05-15 10 views
1

私はTextInputLayout + TextInputEditTextとスピナーでフォームを持っています。私はスピナーの高さのテキストを折り返したいが、ドロップダウンに大きな項目がある。問題は、スピナーの高さがドロップダウンアイテムの高さ(simple_spinner_dropdown_item)に依存することです。 スピナーの下に行を追加するために、style = "@ style/Base.Widget.AppCompat.Spinner.Underlined"を設定しました。スピナーのようなedittext

例:レジデンシャルフィールド enter image description here

+0

[mcve]を入力してください。 –

+0

@ Code-Apprenticeは例をアップロードしました – Yamila

+0

スピナーアイテムはどうですか?それらの値は静的ですか? – FAT

答えて

3

スピナーのEditText

のようなあなたはのEditTextのような、それは。あなたがそのようなカスタムあなたのAppCompatAutoCompleteTextViewできようAutoCompleteTextViewだスピナーたい場合:

public class AutoCompleteDropDown extends AppCompatAutoCompleteTextView { 
    // implements AdapterView.OnItemClickListener 
    private static final int MAX_CLICK_DURATION = 200; 
    private long startClickTime; 
    private boolean isPopup; 
    private int mPosition = ListView.INVALID_POSITION; 

    public AutoCompleteDropDown(Context context) { 
     super(context); 
//  setOnItemClickListener(this); 
    } 

    public AutoCompleteDropDown(Context arg0, AttributeSet arg1) { 
     super(arg0, arg1); 
//  setOnItemClickListener(this); 
    } 

    public AutoCompleteDropDown(Context arg0, AttributeSet arg1, int arg2) { 
     super(arg0, arg1, arg2); 
//  setOnItemClickListener(this); 
    } 

    @Override 
    public boolean enoughToFilter() { 
     return true; 
    } 

    @Override 
    protected void onFocusChanged(boolean focused, int direction, 
            Rect previouslyFocusedRect) { 
     super.onFocusChanged(focused, direction, previouslyFocusedRect); 
     if (focused) { 
      performFiltering("", 0); 
      InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(getWindowToken(), 0); 
      setKeyListener(null); 
      dismissDropDown(); 
     } else { 
      isPopup = false; 
     } 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_UP: { 
       if (isPopup) { 
        dismissDropDown(); 
       } else { 
        requestFocus(); 
        showDropDown(); 
       } 
       break; 
      } 
     } 

     return super.onTouchEvent(event); 
    } 

    @Override 
    public void showDropDown() { 
     super.showDropDown(); 
     isPopup = true; 
    } 

    @Override 
    public void dismissDropDown() { 
     super.dismissDropDown(); 
     isPopup = false; 
    } 



    @Override 
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { 
     Drawable dropdownIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_expand_more_black_18dp); 
     if (dropdownIcon != null) { 
      right = dropdownIcon; 
      right.mutate().setAlpha(66); 
     } 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
      super.setCompoundDrawablesRelativeWithIntrinsicBounds(left, top, right, bottom); 
     } else { 
      super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); 
     } 

    } 



    public int getPosition() { 
     return mPosition; 
    } 
} 

およびic_expand_more_black_18dp.pngはそのようなイメージです。

レイアウト:

<android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:textColorHint="@color/gray_text_hint" 
     app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout.Dark"> 

     <yourpackage.AutoCompleteDropDown 
      android:id="@+id/edtBloodType" 
      style="@style/edt_dark" 
      android:hint="Blood Type" 
      android:inputType="textNoSuggestions" /> 
    </android.support.design.widget.TextInputLayout> 

結果(あなたはAutoCompleteTextView用のアダプタを設定することができます)

enter image description here

+0

あまりに複雑すぎる:S – Yamila

+0

私はedittextとしてスピナーをするために多くを探しました。別の簡単な方法がある場合。それを共有してください。 –

+0

上の私のソリューションを確認してください。http://stackoverflow.com/a/43980904/2516829 – Yamila

-1

ANSWER:私はちょうど設定しました(両方のレイアウトを試しましたが)特定のドロップダウンレイアウトがあります。

ArrayAdapter<String> typeAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.spinner_item, types); 
typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

spinner_itemは左パディングを削除するカスタム・レイアウトです:また

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1" 
style="?android:attr/spinnerItemStyle" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:ellipsize="marquee" 
android:paddingEnd="0dp" 
android:paddingLeft="0dp" 
android:paddingStart="0dp" 
android:singleLine="true" 
android:textAlignment="inherit" /> 

、スピナーにこのスタイルを追加します。ここでは

style="@style/Widget.AppCompat.Spinner.Underlined" 

そして

0

は、回避策です:

レイアウト android.R.layout.simple_spinner_dropdown_itemdropdown項目とカスタムレイアウト custom_spinner_itemのために使用されている。ここ

のみTextViewを表示するスピナーviewために使用されています。

custom_spinner_item.xml:あなたは、あなたのニーズに従ってこのTextViewをカスタマイズすることができFYI

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:maxLines="1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ellipsize="marquee" /> 

希望すると便利です〜

関連する問題