Android微调器垂直偏移量已更改
问题描述:
我在标题下使用Spinner
(TextView
)。它最初设置为View.GONE
,当标题被点击时,Spinner
设置为View.VISIBLE
,弹出窗口使用标题下方的performClick()
显示,这正是我想要的。Android微调器垂直偏移量已更改
但我异步更新BaseAdapter
,以便在仍然是VISIBLE
时添加更多项目。更新后,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>
答
确定。 我试过在移动设备上稍稍调整一些问题,我发现没有问题。 要模拟您的异步添加项目,我添加了一个按钮。它的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;
}
}
我没有找到微调重叠的标题或它在所有移动。
它工作正常。
如果你愿意,我会给你发送截图。 请告诉我,如果你面临任何其他问题
答
我真的找不到解决方案。但通过设置固定高度为微调如上所述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...
}
你可以把你的整体布局? –
添加了完整的布局 – crtn
尝试使用View.Invisible而不是View.Gone,因为它占用了后面的空间。这将帮助您解决覆盖问题;) –