将微调器宽度设置为当前项宽度

时间:2016-10-12 23:28:52

标签: android width spinner selection

默认情况下,微调器宽度设置为适合下拉列表中的最大项目,但我希望它与所选项目的宽度相同。

<android.widget.Spinner
    android:id="@+id/tab_spinner"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:entries="@array/countries" />

enter image description here

正如您在右侧图片中看到的那样,由于列表中的长项目,微调器太长了。

如何将其调整为所选项目宽度?

3 个答案:

答案 0 :(得分:13)

默认情况下,Spinner会尝试测量一些下拉视图并使用找到的最大宽度。这种情况发生在Spinner#measureContentWidth() protected Spinner Spinner#onMeasure()调用SpinnerAdapter#getView()中。

解决此问题的一种方法是确保您的Spinner#getSelectedItemPosition()方法始终使用position作为其xml参数。

我可以想到两种可能的解决方案:

  1. 使用包装器适配器创建自定义微调器(我更喜欢此解决方案)
  2. 或调整自定义适配器

  3. 1。使用包装器适配器创建自定义微调器

    请务必在<Spinner>布局中使用此内容,而不是普通的DynamicWidthSpinner#getAdapter()

    请注意WrapperSpinnerAdapter将返回getBaseAdapter();使用其public class DynamicWidthSpinner extends Spinner { public DynamicWidthSpinner(Context context) { super(context); } public DynamicWidthSpinner(Context context, AttributeSet attrs) { super(context, attrs); } public DynamicWidthSpinner(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void setAdapter(SpinnerAdapter adapter) { super.setAdapter(adapter != null ? new WrapperSpinnerAdapter(adapter) : null); } public final class WrapperSpinnerAdapter implements SpinnerAdapter { private final SpinnerAdapter mBaseAdapter; public WrapperSpinnerAdapter(SpinnerAdapter baseAdapter) { mBaseAdapter = baseAdapter; } public View getView(int position, View convertView, ViewGroup parent) { return mBaseAdapter.getView(getSelectedItemPosition(), convertView, parent); } public final SpinnerAdapter getBaseAdapter() { return mBaseAdapter; } public int getCount() { return mBaseAdapter.getCount(); } public View getDropDownView(int position, View convertView, ViewGroup parent) { return mBaseAdapter.getDropDownView(position, convertView, parent); } public Object getItem(int position) { return mBaseAdapter.getItem(position); } public long getItemId(int position) { return mBaseAdapter.getItemId(position); } public int getItemViewType(int position) { return mBaseAdapter.getItemViewType(position); } public int getViewTypeCount() { return mBaseAdapter.getViewTypeCount(); } public boolean hasStableIds() { return mBaseAdapter.hasStableIds(); } public boolean isEmpty() { return mBaseAdapter.isEmpty(); } public void registerDataSetObserver(DataSetObserver observer) { mBaseAdapter.registerDataSetObserver(observer); } public void unregisterDataSetObserver(DataSetObserver observer) { mBaseAdapter.unregisterDataSetObserver(observer); } } } 方法访问您的适配器。

    parent


    2。调整自定义适配器

    请注意getView()中的Spinner可能并非总是private class SimpleSpinnerAdapter extends BaseAdapter { private LayoutInflater mInflater; private int mResource; public SimpleSpinnerAdapter(Context context, int resource) { mInflater = LayoutInflater.from(context); mResource = resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource( mInflater, ((Spinner) parent).getSelectedItemPosition(), convertView, parent, mResource); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return createViewFromResource( mInflater, position, convertView, parent, mResource); } protected View createViewFromResource(LayoutInflater inflater, int position, View convertView, ViewGroup parent, int resource) { View view; if (convertView == null) { view = inflater.inflate(resource, parent, false); } else { view = convertView; } bindView(position, view); return view; } protected void bindView(int position, View view) { // Bind your view. } // getCount(), getItem(), and getItemId() methods. }

    current

答案 1 :(得分:-1)

  ViewGroup.LayoutParams spinnerLayoutParams = spinner.getLayoutParams();
    spinnerLayoutParams.width -= 1;
   spinner.setLayoutParams(spinnerLayoutParams);

这是选择项目

答案 2 :(得分:-4)

将微调器XML设置为:

android:layout_width="wrap_content"

为我工作。