2012-10-27 31 views
6

Tôi cần tạo bố cục đơn giản với hai tiện ích biểu mẫu (ví dụ - hai nút). Người đầu tiên phải điền vào tất cả chiều rộng có sẵn của bố cục gốc và bố cục thứ hai phải có một số kích thước cố định.Android: Cách tạo bố cục đơn giản này?

Đó là những gì tôi cần: enter image description here

Nếu tôi đặt FILL_PARENT để các phụ tùng đầu tiên - Tôi không thấy cái thứ hai. Nó chỉ thổi ra khỏi một khu vực xem của bố trí :) Tôi không biết làm thế nào để sửa lỗi này ...

Trả lời

14

Cách đơn giản nhất để làm điều này là sử dụng layout_weight với LinearLayout. Lưu ý chiều rộng của TextView đầu tiên là "0dp", có nghĩa là "bỏ qua tôi và sử dụng trọng lượng". Trọng lượng có thể là bất kỳ số nào; vì đó là chế độ xem có trọng số duy nhất nên nó sẽ mở rộng để lấp đầy không gian có sẵn.

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 
    <TextView 
     android:layout_width="25dp" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 
+0

Cảm ơn! Đó chính xác là những gì tôi cần! – JavaRunner

1

Bạn có thể thực hiện điều đó bằng RelativeLayout hoặc FrameLayout.

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp" 
     android:layout_marginLeft="5dp" 
     android:background="#ccccee" 
     android:text="A label. I need to fill all available width." /> 

    <TextView 
     android:layout_width="20dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp" 
     android:paddingRight="5dp" 
     android:background="#aaddee" 
     android:text=">>" /> 

</RelativeLayout> 

How the layout looks

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