2017-07-27 10 views
0

私はタイトルの下にSpinnerを使用しています(TextView)。最初はView.GONEに設定され、タイトルがクリックされるとSpinnerView.VISIBLEに設定され、タイトルの下にperformClick()というポップアップウィンドウが表示されます。Androidスピナーの垂直オフセットが変更されました

しかし、まだVISIBLEである場合、を非同期に更新して、Spinnerに項目を追加しました。更新後、Spinnerは上に移動し、タイトルに重なっています。これをどうすれば解決できますか?

私はandroid:dropDownVerticalOffsetを使用しましたが、更新後も同じ動作を示しています。

マイレイアウト:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:id="@+id/some_other_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
    </LinearLayout> 

    <android.support.v7.widget.AppCompatSpinner 
     android:id="@+id/spinner" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:animateLayoutChanges="true" 
     android:background="@null" 
     android:overlapAnchor="true" 
     android:spinnerMode="dropdown" 
     android:visibility="gone"></android.support.v7.widget.AppCompatSpinner> 
</FrameLayout> 
</LinearLayout> 
+0

レイアウト全体を配置できますか? –

+0

完全なレイアウトを追加しました – crtn

+0

View.Goneの代わりにView.Goneの代わりにView.Instibleを使用してください。これはオーバーレイする問題を解決するのに役立ちます;) –

答えて

0

[OK]をクリックします。 私は携帯電話で若干の調整をして問題を実行しようとしましたが、問題は見つかりませんでした。 アイテムの非同期追加をシミュレートするために、ボタンを追加しました.OnClickはアイテムをアダプタに追加します。 次に、BaseAdapterを拡張する代わりに、ArrayAdapterを使用しました。 そして、LinearLayoutにウェイトを追加してレイアウトの見栄えを助けました。ここで

のレイアウトです:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
> 


     <TextView 
      android:id="@+id/title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Title" 
      android:textSize="20dp" 
      android:gravity="center" 
      android:textColor="@android:color/black" 
     /> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      > 

      <LinearLayout 
       android:id="@+id/some_other_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 
      </LinearLayout> 

      <android.support.v7.widget.AppCompatSpinner 
       android:id="@+id/spinner" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:animateLayoutChanges="true" 
       android:background="@null" 
       android:overlapAnchor="true" 
       android:spinnerMode="dropdown"></android.support.v7.widget.AppCompatSpinner> 
     </FrameLayout> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:text="Load Adapter" 
     android:id="@+id/button" 
    /> 

</LinearLayout> 

そして、ここでは、コードは活動のためです:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_layout); 
    } 


    Spinner spinner; 
    Button button; 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     spinner = (Spinner) findViewById(R.id.spinner); 
     button = (Button) findViewById(R.id.button); 

     List<String> list = new ArrayList<>(); 
     list.add(getRandomStringInRange('A','Z',5)); 
     ArrayAdapter<String> adapter= new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,list); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ArrayAdapter<String> adapter = (ArrayAdapter<String>) spinner.getAdapter(); 
       adapter.add(getRandomStringInRange('A','Z',5)); 
      } 
     }); 

    } 
    public int getRandomNumberInRange(int lower,int higher){ 
     int range = higher-lower; 
     range=(int)(range*Math.random()); 
     return lower + range; 
    } 

    public String getRandomStringInRange(char lower,char higher,int length){ 

     String str =""; 
     for(int i=0;i<length;i++) 
      str+=(char)(getRandomNumberInRange(lower,higher)); 
     return str; 
    } 

} 

私はスピナーのタイトルを重ねるか、それがすべてで動いて見つけることができませんでした。

正常です。

必要に応じて、スクリーンショットをお送りします。 あなたが他の問題に直面している場合は教えてください

0

私は本当にこれの解決策を見つけることができませんでした。しかし、上記のようにスピナーの固定高さを設定することで解決しますin this solution.

Spinner spinner = (Spinner) findViewById(R.id.spinner); 
try { 
    Field popup = Spinner.class.getDeclaredField("mPopup"); 
    popup.setAccessible(true); 

    // Get private mPopup member variable and try cast to ListPopupWindow 
    android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner); 

    // Set popupWindow height to 500px 
    popupWindow.setHeight(500); 
} 
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) { 
    // silently fail... 
} 
関連する問題