2011-01-16 34 views
5

Là một phần của giao diện người dùng lớn hơn, tôi có một số RelativeLayout với ba Chế độ xem mà tôi muốn có cùng chiều rộng. Ba quan điểm được "chồng" trên đầu trang của mỗi khác như thế nàyCách tạo nhiều Chế độ xem có cùng chiều rộng?

  1. ImageView
  2. TextView
  3. ImageView

Tôi đã thử mọi thứ khác nhau như thiết lập các View s'android:layout_width để "wrap_content", đặt android:layout_width thành "0px" và android:layout_weight thành "1" theo đề xuất của this answer và đặt View s trong một LinearLayout không thành công.

Làm cách nào để có được ba chế độ xem này có cùng chiều rộng?

phần có liên quan của xml bố trí:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0px" 
    android:layout_weight="1"> 
     <ImageView 
      android:layout_above="@string/window_text" 
      android:layout_below="@string/eighteen_id" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:paddingTop="5dp" 
      android:layout_alignParentRight="true" 
      android:paddingBottom="5dp" 
      android:src="@drawable/line1" 
      android:id="@string/line_1_id"/> 
     <TextView 
      android:layout_above="@string/line_2_id" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:gravity="right" 
      android:layout_centerVertical="true" 
      android:textColor="#000000" 
      android:id="@string/window_text"/> 
     <ImageView 
      android:layout_above="@string/top_row_id" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_alignParentRight="true" 
      android:paddingTop="5dp" 
      android:paddingBottom="10dp" 
      android:src="@drawable/line1" 
      android:id="@string/line_2_id"/> 
</RelativeLayout> 
+0

Bạn đang hiển thị một RelativeLayout, không phải là một LinearLayout? –

+0

Tôi có một vài 'Chế độ xem' khác trong «RelativeLayout' này sử dụng vị trí tương đối. –

Trả lời

19

Điều này rất đơn giản, xem bên dưới. "Ma thuật" rất đơn giản. Đặt parent thành wrap_content và khung nhìn con để điền vào parent. Các chế độ xem con nhỏ hơn sau đó luôn được đặt thành kích thước của chế độ xem con lớn nhất.

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

    <LinearLayout 
     android:id="@+id/bottom_row" 
     android:orientation="vertical" 
     android:layout_alignParentBottom="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/view1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="123" /> 

     <Button 
      android:id="@+id/view2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="more text" /> 

     <Button 
      android:id="@+id/view3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="even more text" /> 
    </LinearLayout> 
</RelativeLayout> 

alt text

1

Bạn có thử thiết android: layout_width = "fill_parent" đối với các quan điểm hai hình ảnh, giống như bạn có nó cho TextView? Nếu bạn muốn ba chế độ xem là chiều rộng của chiều rộng nhất, bạn có thể bọc cả ba chế độ xem trong LinearLayout có android: layout_width = "wrap_content".

+0

Tôi đã thử cả hai đề xuất của bạn nhưng chúng không hoạt động. Văn bản sẽ tràn hình ảnh ở cả bên phải và bên trái. –

0

Bạn có thể nhận được hình ảnh để lấp đầy chiều rộng gốc bằng cách đặt android: scaleType = "" trong ImageView nhưng nếu văn bản không lấp đầy chiều rộng màn hình và bạn muốn chiều rộng hình ảnh khớp chính xác với chiều rộng của văn bản, bạn sẽ phải làm điều đó một cách lập trình bằng cách lấy chiều rộng khung nhìn văn bản và sau đó đặt chiều rộng của hình ảnh đó thành chiều rộng của khung hình.

+0

Tôi chạy vào loại ngược lại. Tôi muốn TextView của tôi không mở rộng chiều rộng của chúng vượt quá chiều rộng của ImageView của tôi. TextViews, khi được đặt thành wrap_content, rộng hơn so với ImageView. Vì vậy, tôi đã phải thiết lập chiều rộng của họ trong mã. Một mục layout_matchWidth sẽ rất hay khi có trong RelativeLayout. – Mark

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