【问题标题】:Android spinner vertical offset changedAndroid微调器垂直偏移已更改
【发布时间】:2017-08-15 20:43:54
【问题描述】:

我在标题下方使用Spinner (TextView)。它最初设置为View.GONE,当点击标题时,Spinner 设置为View.VISIBLE,并且在标题下方使用performClick() 显示弹出窗口,这正是我想要的。

但是当Spinner 仍然是VISIBLE 时,我会异步更新BaseAdapter 以向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>

【问题讨论】:

  • 你能把你的整个布局放上去吗?
  • 添加了完整的布局
  • 尝试使用 View.Invisible 而不是 View.Gone,因为它会占用空间。这将帮助您解决重叠问题;)
  • 它显示与 View.INVISIBLE 相同的行为。只有在添加更多项目以适应可见屏幕时,才会发生这种情况。
  • 不幸的是,我现在无法测试,我没有正确的 IDE atm,但您可以尝试让微调器可见,看看它第一次创建的位置,以确保它位于正确的位置,因为我可以看到框架布局内的错误有一个额外的线性布局,基本上什么都不做。

标签: android spinner


【解决方案1】:

好的。 我尝试在我的手机上进行一些细微的调整来解决您的问题,但我没有发现任何问题。 为了模拟您的异步添加项目,我添加了一个按钮。它的 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;
    }

}

我没有发现微调器与标题重叠或根本没有移动。

一切正常。

如果你愿意,我会把截图发给你。 如果您遇到任何其他问题,请告诉我

【讨论】:

    【解决方案2】:

    我真的找不到解决方案。但是通过为微调器设置固定高度来解决,如提到的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...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多