2012-01-02 33 views
5

Tôi đang cố gắng tạo giao diện, trong đó hai hoặc nhiều nút sẽ được hiển thị cho người dùng, nếu một nút được nhấp, một số Bố cục sẽ được hiển thị cho anh ấy. Tôi đang sử dụng SlidingDrawer cho mục đích này.Android layout_width & layout_height, cách hoạt động?

Vâng, tôi bị nhầm lẫn bởi layout_width & layout_height properties.

Nếu tôi đặt các thuộc tính như bên dưới, chỉ hiển thị "Xử lý 1" trên màn hình.

android:layout_width="fill_parent" android:layout_height="wrap_content" 

Thành thật mà nói, tôi không có đủ kiến ​​thức về cả hai thuộc tính này. Ai đó có thể chia sẻ kiến ​​thức của họ về họ không?

main.xml:

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

    <SlidingDrawer android:id="@+id/slidingDrawer1" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:handle="@+id/handle1" 
      android:content="@+id/content1"> 
       <Button android:text="Handle 1" android:layout_height="wrap_content" 
         android:layout_width="fill_parent" android:id="@+id/handle1"></Button> 
       <LinearLayout android:id="@+id/content1" 
         android:layout_width="fill_parent" android:layout_height="wrap_content" 
         android:orientation="vertical" android:gravity="center" 
         android:background="#FF444444"> 
         <Button android:text="Handle 1 Item 1" android:layout_height="wrap_content" 
           android:layout_width="wrap_content" android:id="@+id/item1"></Button> 
       </LinearLayout> 
    </SlidingDrawer> 

    <SlidingDrawer android:id="@+id/slidingDrawer2" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:handle="@+id/handle2" 
      android:content="@+id/content2"> 
       <Button android:text="Handle 2" android:layout_height="wrap_content" 
         android:layout_width="fill_parent" android:id="@+id/handle2"></Button> 
       <LinearLayout android:id="@+id/content2" 
         android:layout_width="fill_parent" android:layout_height="wrap_content" 
         android:orientation="vertical" android:gravity="center" 
         android:background="#FF444444"> 
         <Button android:text="Handle 2 Item 1" android:layout_height="wrap_content" 
           android:layout_width="wrap_content" android:id="@+id/item2"></Button> 
       </LinearLayout> 
    </SlidingDrawer>  
</LinearLayout> 
+5

Bạn đã kiểm tra [tài liệu] (http: // developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT)? Nó khá rõ ràng. –

+1

Câu hỏi hay. Tôi đã nhầm lẫn về điều này một vài tháng trước khi tôi lần đầu tiên bắt đầu. Cho nó một vài ngày, và bạn sẽ bị treo nó. – Reid

Trả lời

5

Các layout_widthlayout_height tính chất của một cái nhìn được dự định sẽ được sử dụng bằng container cha của nó. Một số vùng chứa bỏ qua một hoặc cả hai loại này; tôn vinh họ nhất. Bạn cần tham khảo tài liệu về thùng chứa (trong trường hợp của bạn, SlidingDrawer) để hiểu cách sử dụng các giá trị.

Bạn không hiển thị tệp main.xml hoàn chỉnh, vì vậy thật khó để nói chính xác điều gì đang xảy ra. Nó cũng sẽ giúp đỡ nếu bạn đăng một hình ảnh của những gì là sai trái.

EDIT

Sau khi nhìn thấy bố cục hoàn chỉnh của bạn, tôi nghĩ rằng vấn đề cơ bản ở đây là bạn đang sử dụng một LinearLayout chứa các SlidingDrawers. Theo ghi chú docs for SlidingDrawer, chúng cần nằm trong FrameLayout hoặc RelativeLayout (thực ra, bất kỳ vùng chứa nào cho phép nhiều chế độ xem nằm trên cùng nhau).

Một khả năng khác là SlidingDrawer thứ hai đang được đặt trực tiếp dưới chế độ đầu tiên. Hãy thử thay đổi kích thước của nút thứ hai (ví dụ: làm cho văn bản dài hơn) và xem liệu nó có xuất hiện ở hai bên của nút hay không 1.

+0

Tôi đã cung cấp tệp main.xml hoàn chỉnh, vui lòng xem câu hỏi của tôi. –

+0

@YaqubAhmad - Tôi đã cập nhật câu trả lời của mình. –

+0

Cảm ơn rất nhiều. Hãy để tôi kiểm tra nó. –

4

fill_parent = match_parent và điều đó có nghĩa là nó có chiều rộng hoặc chiều cao (bao giờ sở hữu nó đang được quy định như) của "cha mẹ" container

wrap_content có nghĩa là chiều cao hoặc chiều rộng mất vào chiều cao hoặc chiều rộng của "đứa trẻ"

Bạn muốn sử dụng trọng lượng thay vì sử dụng match_parent hoặc wrap_content thông thường.

Khi bạn sử dụng trọng số bạn muốn đặt chiều rộng thành 0dp.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/main" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

    <LinearLayout 
    android:id="@+id/lowerLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="1.0" > 
      <LinearLayout 
      android:id="@+id/headerLayout" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight=".7" > 
       <Button 
        android:id="@+id/previousMonthButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Previous" /> 

       <TextView 
        android:id="@+id/monthName" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:text="Large Text" 
        android:layout_weight="1" 
        android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_horizontal" /> 

       <Button 
        android:id="@+id/nextMonthButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Next" /> 

      </LinearLayout> 

      <LinearLayout 
      android:id="@+id/lowerLayout3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight=".3" > 

       <Button 
        android:id="@+id/addEvent" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

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

     <LinearLayout 
      android:id="@+id/monthView" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:layout_weight=".7" > 

     </LinearLayout> 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".3" > 
     </ListView> 
    </LinearLayout> 

</LinearLayout> 
+0

Tôi biết điều này, nhưng nhìn vào xml được cung cấp của tôi, tại sao nó chỉ hiển thị "Xử lý 1", nếu quy tắc trên được áp dụng, nó sẽ hiển thị cả "Xử lý 1" và "Xử lý 2". –

+0

Bạn đang điền phụ huynh hai lần. Hãy thử sử dụng trọng số thay vì đặt chiều rộng bố cục thành fill_parent. Cũng xin lưu ý rằng fill_parent không được chấp nhận; sử dụng match_parent thay thế. Tôi đã chỉnh sửa câu trả lời của mình để tôi có thể cho bạn thấy một ví dụ về điều này. – Reid

0

Tôi nghĩ bạn đang cố gắng thêm 2 Ngăn kéo trượt.

Kích thước của SlidingDrawer xác định bao nhiêu không gian nội dung sẽ chiếm một lần trượt ra nên SlidingDrawer thường nên sử dụng match_parent cho cả hai chiều của nó trong trường hợp này chồng lên nhau để bạn không thể thấy trình xử lý 2.

Nếu bạn đặt chế độ hiển thị "đã biến mất" của SlidingDrawer đầu tiên, bạn có thể thấy mục Khác.

Cập nhật

Hi,

Vui lòng thử mã này nó có thể giúp bạn

<?xml version="1.0" encoding="utf-8"?> 

<SlidingDrawer 
    android:id="@+id/slidingDrawer1" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" 
    android:content="@+id/content1" 
    android:handle="@+id/handle1" > 

    <Button 
     android:id="@+id/handle1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Handle 1" > 
    </Button> 

    <LinearLayout 
     android:id="@+id/content1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#FF444444" 
     android:gravity="center" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/item1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Handle 1 Item 1" > 
     </Button> 
    </LinearLayout> 
</SlidingDrawer> 
+0

vâng tôi muốn thêm nhiều ngăn kéo trượt, vì nó rõ ràng từ câu hỏi của tôi. Nếu tôi thiết lập khả năng hiển thị đi thì ngăn kéo trượt đầu tiên sẽ trở thành vô hình. –

+0

@YaqubAhmad Kiểm tra bài đăng cập nhật ... – loks

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