2012-10-13 34 views
12

tôi đã cố gắng rất nhiều để thay đổi chiều cao mục thả xuống của spinner .. nhưng tôi không thể có được một giải pháp tốt .. plz giúp tôi guys ..làm thế nào để thay đổi chiều cao thả xuống trong spinner

đây là một mã 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

làm thế nào để thay đổi chiều cao của đơn thả xuống bất kỳ ý tưởng ..

+0

Kiểm tra sau URL cho các giải pháp chính xác: http://stackoverflow.com/questions/8878260/how-to-re- size-the-height-of-the-items-in-spinner-với-multiple-choice – Chase1986

+0

bạn nên đánh dấu bài đăng của John là câu trả lời. –

Trả lời

0

Các bạn đã thử một cái gì đó giống như android:layout_height="30dp" thay vì fill_parent vì một trong hai yếu tố trong loginrowspinner.xml?

+1

s ông chủ nó sẽ thay đổi chiều cao của spinner nó không sử dụng .. tôi muốn chiều cao thả xuống –

+0

cuối cùng tôi đã nhận ông chủ đầu ra .. –

41

Chỉ cần thêm phần này vào bộ điều hợp của bạn.

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

đây là giải pháp đơn giản và tốt nhất. cảm ơn. –

+0

Đây phải là giải pháp được chấp nhận! – Roel

+0

... vì vậy giải pháp thông minh :) – Davidm176

6

Trong loginrowspinner.xml, thêm android:minHeight="48dp" đến TextView phần tử.

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

Tôi đã thử với cách tiếp cận ở trên và điều đó đã thực hiện mẹo ..+1 – user755499

+0

Trường hợp TextView đến từ đâu trong Spinner? –

4

Có lẽ nó có thể giúp ..

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; 
     } 
    }; 
0

thêm này trong bộ chuyển đổi của bạn: convertView.setMinimumHeight (60); làm việc cho tôi.

1

Tạo TextView của riêng bạn trong thư mục bố trí như thế này mà sẽ được áp dụng trong danh sách thả xuống cửa sổ bật lên

<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" /> 

Lưu ý tôi đã cung cấp

android:layout_height="60dp" 

android:paddingLeft="20dp" 

và sử dụng cho spinner của bạn, như thế này

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); 

công trình này cho tôi :)

1

Bạn có thể thử sử dụng 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; 
} 
0

// Điều này cũng có thể giúp đỡ:

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; 
     } 

    }; 
+0

Bạn có thể giải thích ''40 * cx.getResources(). GetDisplayMetrics(). Density''? – backslashN

+0

@backslashN, khi viết mã Java cho Android, bạn không thể sử dụng các đơn vị dp và phải chỉ định bằng pixel thay vì (không giống như XML, cũng chấp nhận dp). Nếu bạn đặt 40px cho tất cả các trường hợp, chiều cao sẽ vẫn trông nhỏ trên màn hình với nhiều pixel. Đó là lý do tại sao chúng tôi sử dụng mã này để lấy mật độ thiết bị hiện tại vào tài khoản – kit

Các vấn đề liên quan