2012-01-03 26 views
6

Bố cục của tôi có ImageView (iv) và TextView (tv). Tôi muốn để quyền tv của iv. Và chiều cao của TV giống như chiều cao của iv. TV Gravity được đặt là CENTER_VERTICAL.Cách để ImageView và TextView có cùng chiều cao

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


    <ImageView 
     android:id="@+id/iv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true"/> 


    <TextView 
     android:id="@+id/tv" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/iv" 
     android:text="TextView" android:gravity="center_vertical"/> 

</RelativeLayout> 

Làm cách nào để đạt được mục tiêu?

+1

bạn có thể sử dụng LinearLayout thay vì RelativeLayout, sau đó bạn có thể sử dụng tham số layout_weight như 1 cho cả ImageView và TextView –

+0

Làm thế nào bạn sẽ có cùng chiều cao cho cả hai widget? chiều cao textview phụ thuộc vào kích thước văn bản. Chúng ta chỉ có thể đứng cạnh nhau, chúng ta không thể đảm bảo chiều cao như nhau. –

Trả lời

20

hi thử này .....

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

    <ImageView 
    android:id="@+id/iv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/ic_title_home_demo" /> 

    <TextView 
    android:id="@+id/tv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/iv" 
    android:layout_alignTop="@+id/iv" 
    android:layout_toRightOf="@+id/iv" 
    android:gravity="center_vertical" 
    android:text="TextView" /> 

    </RelativeLayout> 
1

Để làm cho nó chính giữa theo chiều dọc, bạn sẽ cần phải xóa dòng android:layout_alignParentTop="true" khỏi thẻ TextView của mình.

Tuy nhiên, có vẻ như bạn đang cố gắng thay đổi kích thước văn bản để phù hợp với toàn bộ chiều cao của ImageView. Điều này rõ ràng sẽ không giải quyết điều đó, nhưng tôi sẽ không khuyên bạn nên làm điều đó ngay từ đầu vì nó có thể dẫn đến một số quan điểm kỳ lạ.

1

Bạn có thể sử dụng các kích cỡ khác nhau bằng cách sử dụng

android: layout_width = "wrap_content"

và/hoặc

android: height = "300dp"

(tương tự cho chiều cao). Ngoài ra kiểm tra lề và paddings.

Nếu bạn cần phải năng động hơn, bạn có thể xem xét android: layout_weight tức là đã cho 2 nút có chiều rộng bằng nhau và lấp đầy khoảng trống ngang của xem mẹ bạn sẽ thiết lập:

android: layout_height =" wrap_content "

android_layout: weight =" 0.5 ".

bạn có thể đi theo liên kết này: How to make two different layout to have same height?

4

Bạn phải thiết lập thuộc tính TextView để android:layout_alignTop="@id/iv"android:layout_alignBottom="@id/iv"

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