2009-07-24 47 views
69

Tôi muốn tạo hộp thoại để hiển thị tiêu đề và thẻ video. Văn bản bên dưới Tôi muốn thêm các nút Xem, Chỉnh sửa và Xóa và làm cho các thành phần này có cùng kích thước. Có ai biết làm thế nào để sửa đổi tập tin bố trí .xml để làm cho các yếu tố bên trong LinearView cùng kích thước?Android: Làm thế nào để làm cho tất cả các phần tử bên trong LinearLayout cùng kích thước?

File bố trí hiện tại trông như thế này:

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

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtTitle" android:text="[Title]" > 
      </TextView> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtTags"    
       android:text="[Tags]" > 
      </TextView> 

    </LinearLayout> 

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

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnPlay" 
      android:text="View"> 
     </Button> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnEdit" 
      android:text="Edit"> 
     </Button> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnDelete" 
      android:text="Delete"> 
     </Button> 

    </LinearLayout> 

</LinearLayout> 

tôi sẽ đánh giá cao nếu có ai có thể hiển thị các giải pháp bằng cách sửa đổi nội dung tập tin dán.

Cảm ơn!

Trả lời

148

Sử dụng android:layout_width="0px"android:layout_weight="1" trên ba Button s. Điều đó nói rằng các nút sẽ chiếm không quá 0 pixel, nhưng ba nút này sẽ phân chia thêm khoảng trống giữa chúng. Điều đó sẽ cho bạn hiệu ứng hình ảnh mà bạn muốn.

+4

Nếu bạn _REALLY_ muốn bạn, bạn có thể sử dụng TableLayout với android: stretchColumns = "0,1,2". –

+0

Tuyệt vời ... vì vậy điều này có nghĩa, nếu chúng ta đặt chiều rộng là 0px, trọng lượng sẽ ghi đè cài đặt? Tại sao nó như vậy? – noob

+0

Điều này làm việc hoàn hảo. – Proverbio

28

Một cách khác là tạo android:layout_width="fill_parent"android:layout_weight="1" điều này cũng sẽ hoạt động tốt !!!

+13

Bạn thực sự vui mừng về câu trả lời này. :-) Yêu thích sự nhiệt tình! –

+0

Giải pháp của CommonsWare không hiệu quả đối với tôi, nhưng điều này đã làm được! :) Tôi đã sử dụng "match_parent" vì tôi nghe bạn nên thích nó hơn "fill_parent", nhưng cả hai đều hoạt động. – codepleb

1

Tôi đã viết một EqualSpaceLayout mà tôi nghĩ sẽ giải quyết được vấn đề của bạn. Điều này tương tự như LinearLayout, nhưng nó không làm méo mó kích thước của các khung nhìn con. Hãy khám phá: http://www.dev-smart.com/archives/225

16

Sử dụng LinearLayout với mong muốn weightSum và tạo các thành phần có số lượng bằng layout_weight. Dưới đây là một ví dụ ...

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="5"> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_share_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_search_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_event_note_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_brush_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_menu_white_36dp"/> 
</LinearLayout> 

Vì vậy, tổng trọng lượng của tất cả các yếu tố là 5. Dưới đây là ảnh chụp màn hình ...

enter image description here

Lưu ý rằng, Google Material Design biểu tượng được sử dụng. Hy vọng điều này là hữu ích.

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