2013-07-08 23 views
8

Tôi đang gặp một số vấn đề khi nhận hai con của số LinearLayout để có cùng chiều rộng. Đây là những gì tôi nhận được:LinearLayout với hai con có chiều rộng bằng nhau

screenshot

Và đây là xml bố trí của tôi cho các hộp màu xám:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="2" 
    android:background="@color/medium_grey" 
    > 

    <ImageView 
     android:id="@+id/profile_photo" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/placeholder_profile_photo" 
     android:scaleType="fitCenter" 
     android:contentDescription="@string/blank" 
     android:layout_weight="1" 
     /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center" 
     android:layout_weight="1" 
     android:layout_gravity="center_vertical" 
     android:background="@color/alert" 
     > 

     <TextView 
      android:id="@+id/profile_rate_header" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Rate User" 
      /> 

     <LinearLayout 
      android:id="@+id/profile_action_rate_user" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1" 
      android:gravity="center" 
      > 

      <ImageView 
       android:id="@+id/profile_action_rate_up" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/user_rate_up" 
       /> 

      <ImageView 
       android:id="@+id/profile_action_rate_down" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/user_rate_down" 
       /> 

     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

tôi đã giả định rằng thiết lập layout_weight của con cái gốc LinearLayout cùng với a weightSum và chiều rộng 0dp sẽ tạo ra hiệu ứng mong muốn (hình ảnh có cùng kích thước với phần 'tỷ lệ người dùng' màu hồng) tuy nhiên đây không phải là trường hợp.

Tôi đang thiếu gì?

Edit:

Đây là những gì tôi muốn nó trông giống như

enter image description here

Bức ảnh và cách bố trí tuyến tính màu hồng nên độ rộng bằng nhau.

+0

tôi đã không nhận được sự cố hoàn chỉnh của bạn @ boz.plz cho biết, đầu ra mong muốn của bạn là gì ??? tôi có thể giải quyết vấn đề cân nặng của bạn. tôi nghĩ bạn nên sử dụng trọng lượng được sắp xếp của weightSum. – Rumit

+0

@Rumit Tôi đã thêm một số thông tin khác – boz

+0

có thể trùng lặp của [Android: Làm thế nào để làm cho tất cả các yếu tố bên trong LinearLayout cùng kích thước?] (Http://stackoverflow.com/questions/1177020/android-how-to-make-all- các yếu tố-bên trong-linearlayout-cùng kích thước) –

Trả lời

19

android:weightSum="2" phải là cha mẹ của hai trẻ em ImageViews, không phải trên cha mẹ trên. Hoặc cố gắng thiết lập weightsas 0.5 và xem nó có hoạt động không.

Ngoài ra, chiều rộng của hai chế độ xem hình ảnh phải là android:layout_width="0dp" khi sử dụng các trọng số như thế này.

Tiếp theo, mở rộng hình ảnh của bạn để lấp đầy không gian. Chi tiết here.

+0

Ok đã tạo ra một chút khác biệt. Hình ảnh và con 'LinearLayout' bây giờ có chiều rộng tương tự nhưng chúng không Dưới đây là một ảnh chụp màn hình http://i.stack.imgur.com/UuFHp.png – boz

+0

Xem chỉnh sửa Nhưng hình ảnh bên phải của bạn trông giống như nó không có cơ hội được thu nhỏ theo ý bạn trừ khi bạn nhìn vào việc tạo ra nó là 9-patch – SK9

+0

Nếu tôi muốn thêm đứa trẻ thứ ba mà phải ở dưới cái đầu tiên, nhưng nó không phải là? – TeodorKolev

1

Xóa tổng trọng lượng khỏi Chế độ xem cha mẹ (@+id/profile_action_rate_user).

+0

Tôi vừa thử điều này và nó không có sự khác biệt: ( – boz

0

Bạn có thể sử dụng một cái gì đó như thế này, hy vọng điều này sẽ phù hợp với yêu cầu của bạn. Hãy thử xml này.

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

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

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="@color/Black" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:layout_weight="0.5" 
     > 

     <TextView 
      android:id="@+id/profile_rate_header" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Rate User" 
      /> 

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

      <ImageView 
       android:id="@+id/profile_action_rate_up" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/close" 
       /> 

      <ImageView 
       android:id="@+id/profile_action_rate_down" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/close" 
       /> 

     </LinearLayout> 
    </LinearLayout> 

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

     <ImageView 
      android:id="@+id/profile_photo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleType="fitCenter" 
      android:src="@drawable/close" /> 

    </LinearLayout> 

</LinearLayout> 


</LinearLayout> 
2

này là bạn muốn:

thử xml @boz này: nó sẽ chạy trong tất cả các kích thước của bố trí ...!

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

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight=".50" 
     android:background="@drawable/ic_launcher" > 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight=".50" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" > 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" 
      android:background="@android:color/darker_gray" > 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="33.33" > 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 

tôi mất hai bố trí tuyến tính với (50%, 50%), và ba bố trí tuyến tính đứa trẻ (33,33%, 33,33%, 33,33%). Trong đó ba bố trí, bố trí 2 là pinkone của bạn.

nếu vẫn còn bất kỳ truy vấn nào, hãy hỏi.

6

trẻ em kém trọng

Để tạo một bố trí tuyến tính trong đó mỗi đứa trẻ sử dụng cùng một lượng không gian trên màn hình, thiết lập android: layout_height của mỗi chế độ xem "0dp" (dành cho bố trí dọc) hoặc android: layout_width của mỗi chế độ xem thành "0dp" (cho bố cục ngang). Sau đó, đặt android: layout_weight của mỗi chế độ xem thành "1". Tham khảo: http://developer.android.com/guide/topics/ui/layout/linear.html

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