如何在微调器中更改下拉高度

时间:2012-10-13 05:32:20

标签: android android-layout android-intent android-spinner

我尝试了很多来改变微调器的下拉项目高度..但我无法得到一个很好的解决方案.. PLZ帮助我们..

这是一个代码 loginactivityview.xml

       <Spinner
         android:id="@+id/spinnerFacility"
         android:layout_width="400dip"
         android:layout_height="50dip"
         android:layout_alignLeft="@+id/lpassword"
         android:layout_below="@+id/lpassword"
         android:layout_marginTop="32dip"            
         android:background="@drawable/location" 
         android:paddingLeft="10dip"               
         android:dropDownWidth="@style/dropDown"  
         android:minHeight="40dip"     
         android:typeface="monospace" />

loginrowspinner.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

  <TextView
    android:id="@+id/textViewRowFacility"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="left"
    android:paddingBottom="5dip"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:paddingTop="5dip"
    android:text="Facility"
    android:textColor="#000000"
    android:textSize="20dip" >
</TextView>

enter image description here

如何改变下降任何想法的高度..

11 个答案:

答案 0 :(得分:52)

只需将其添加到适配器即可。

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

答案 1 :(得分:7)

在loginrowspinner.xml中,将android:minHeight="48dp"添加到TextView元素。

 <TextView 
     ...
     android:id="@+id/textViewRowFacility"
     android:minHeight="48dp" />

答案 2 :(得分:5)

也许它可以帮助..

ArrayAdapter<String> yourSpinnerAdapter = new ArrayAdapter<String>(this,
                R.layout.spinner_item, yourItem) {

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            convertView = super.getDropDownView(position, convertView,
                    parent);

            convertView.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams p = convertView.getLayoutParams();
            p.height = 100; // set the height
            convertView.setLayoutParams(p);

            return convertView;
        }
    };

答案 3 :(得分:2)

在布局文件夹中创建您自己的textview,将在下拉弹出窗口中填充

<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="60dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="18sp"
android:textAlignment="inherit" />

注意我提供了

android:layout_height="60dp"

android:paddingLeft="20dp"

并将其用于您的微调器,就像这样

Spinner dropdown = (Spinner)findViewById(R.id.sosMode);
String[] items = new String[]{"Date", "Travelling alone"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_dropdown_list, items);
dropdown.setAdapter(adapter);

这适合我:)

答案 4 :(得分:1)

您可以尝试使用dps。

@Override   
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    TextView textview = (TextView)inflater.inflate(android.R.layout.simple_spinner_item, null);

    textview.setText(alOpcion.get(position).getOpcionName());

    DisplayMetrics metrics = parent.getResources().getDisplayMetrics();
    float dp = 40f;
    float fpixels = metrics.density * dp;
    int pixels = (int) (fpixels + 0.5f);

    textview.setHeight(pixels);

    return textview;
}

答案 5 :(得分:0)

您是否曾尝试使用android:layout_height="30dp"而不是fill_parent来登录loginrowspinner.xml中的任何一个元素?

答案 6 :(得分:0)

在适配器中添加:convertView.setMinimumHeight(60);这适合我。

答案 7 :(得分:0)

//这也可能会有所帮助:

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataForAdapter) {


        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            v.setMinimumHeight((int) (40*cx.getResources().getDisplayMetrics().density));
            v.setBackgroundColor(Color.rgb(222, 222, 222));


            return v;
        }

    };

答案 8 :(得分:0)

覆盖ArrayAdapter类中的方法

@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View view = super.getDropDownView(position, convertView, parent);
    view.getLayoutParams().height = 50;
    return view;
}

答案 9 :(得分:0)

遇到了同样的问题。找到了以下对我有用的解决方案:

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        setSpinnerDropDownHeight()
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mBinding?.included?.spinn?.getViewTreeObserver()?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
                @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
                override fun onGlobalLayout() {
                    mBinding?.included?.spinn?.dropDownVerticalOffset = mBinding?.included?.spinn?.getDropDownVerticalOffset()!! + mBinding?.included?.spinn?.getHeight()!!
                    mBinding?.included?.spinn?.viewTreeObserver!!.removeOnGlobalLayoutListener(this)
                }
            })

            mBinding?.included?.spinn?.dropDownVerticalOffset = 10
        }
    }

希望它可以帮助某人!!!!

谢谢

答案 10 :(得分:0)

正如人们所说,最好的方法是添加

    android:minHeight="48dp"

到.xml。但是,如果要以编程方式进行操作,请使用:

    mspinner.setMinimumHeight(48);

使用mspinner作为微调器的名称,并将48作为选项的新高度。