2011-12-20 41 views
7

Nếu tôi thêm spinner trong một LinearLayout ngang hoặc trong một dòng của bảng ví dụ như thế này:Spinner nổi xuống khi ngang thẳng hàng

<LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 
     </EditText> 

     <Spinner 
      android:id="@+id/spinner1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

Các spinner nổi xuống. Nó nổi xuống trên Android 2.2, android 2.3 và android 3.2 nhưng nó hoạt động tốt trong Android 4.0 họ đã sửa nó. Nhưng có cách nào để làm cho nó phù hợp với các Views khác như EditText trên Android 2.3.

By nổi xuống Ý tôi là:

*********************** 
* if EditText is here * ************************ 
*********************** * Spinner will be here * 
         ************************ 
+0

Tôi không thể tạo lại điều này.Làm thế nào lớn là sự khác biệt chiều cao? Chỉ một vài pixel? –

Trả lời

0

Trong LinearLayout, định hướng là "ngang". Các yếu tố ý nghĩa được đặt cạnh nhau. EditText và Spinner của bạn, cả hai đều có android: layout_width = "fill_parent". Vì vậy, bạn muốn đặt EditText & Spinner của bạn cạnh nhau, nhưng bạn cũng muốn họ điền vào ...

Bạn sẽ làm gì nếu bạn đã đưa ra những điều kiện mâu thuẫn này?

Rõ ràng, đây là mâu thuẫn và các phiên bản Android khác nhau đưa ra các ưu tiên khác nhau cho các thuộc tính này.

Tôi sẽ đề nghị thay đổi EditText và Spinner thuộc tính:

<EditText 
    ... 
    android:layout_width="0dp" 
    ... /> 
<Spinner 
    ... 
    android:layout_width="0dp" 
    ... /> 

Và thêm video này vào LinearLayout của bạn:

<LinearLayout 
    android:baselineAligned="true" 
    ... /> 
+0

Tôi đã thử điều này, cùng một điều xảy ra – Mustafa

+0

và thêm android: baselineAligned = "true" trong LinearLayout của bạn. Nếu điều đó không khắc phục được nó, tôi là clueluess :) – Entreco

+1

thực sự thiết lập baselineAligned = "false" của cha linayayout chỉ giải quyết được vấn đề. –

0

Thêm android: baselineAligned = "false" để LinearLayout để algn lần xem . này làm việc trong dự án của tôi:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/text" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <Spinner 
     android:id="@+id/spinner" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout> 
1

Bạn nên thêm dòng sau vào EditText của bạn:

android:layout_gravity="center_vertical" 
3

Spinner nhỏ hơn EditText, do đó, một cách để gắn kết cả hai đó là để thêm android: layout_height = "match_parent" để Spinner, và giữ LinearLayout và EditText với wrap_content.

0

Tôi tìm thấy giải pháp sau đây:

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical"/>    
    <Spinner 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
</LinearLayout> 
2

tôi phải đối mặt với những rắc rối tương tự trước và tôi đã tìm ra câu trả lời sau khi thử và sửa đổi xml của tôi, đây là mã xml của tôi:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/linearlayout1" 
    android:orientation="horizontal" 
    android:gravity="bottom"> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/text_input_layout" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="50"> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:textSize="20dp" 
     android:hint="Duration" 
     android:layout_gravity="left" 
     android:gravity="center" 
     /> 
</android.support.design.widget.TextInputLayout> 
<Spinner 
    android:id="@+id/spinner" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:layout_weight="50" 
    /> 

Chỉ cần đặt gravity trong số LinearLayout thành bottom và sẽ giải quyết được sự cố của bạn.

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