2010-06-11 43 views
14

Tôi đang tải điều hướng từ trang web, trong đó mỗi mục có tiêu đề, mô tả và hình ảnh. Tôi muốn có tất cả các tiêu đề và mô tả tập trung dọc theo cùng một trục.Đặt chiều rộng tối đa của ImageView dưới dạng phần trăm chiều rộng của bố mẹ

Tôi đang sử dụng bố cục sau cho mỗi mục trong một ListView. Nó hoạt động tốt khi hình ảnh rộng bằng một nửa vì chúng cao, nhưng không phải nếu chúng rộng hơn - văn bản đi dưới hình ảnh.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 
      <TextView 
       android:layout_height="wrap_content" 
       android:textSize="10pt" 
       android:id="@+id/title" 
       android:gravity="center_horizontal" 
       android:layout_width="fill_parent" /> 
      <TextView 
       android:layout_height="wrap_content" 
       android:id="@+id/description" 
       android:layout_width="fill_parent" 
       android:gravity="center_horizontal" /> 
     </LinearLayout> 
     <View 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent" 
      android:layout_weight="3" /> 
    </LinearLayout> 
    <ImageView 
     android:id="@+id/icon" 
     android:adjustViewBounds="true" 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 

Có thể buộc hình ảnh ở lại 25% chiều rộng hoặc chiều rộng của RelativeLayout không?

Trả lời

15

Điều này không thể thực hiện được trong XML. Bạn sẽ phải chỉ định kích thước trong mã. Tôi sẽ làm điều đó trong onCreate() của hoạt động:

[Sửa:. Thêm nhập khẩu, ký hiệu float]

import android.widget.LinearLayout.LayoutParams; 
... 
obj.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.WRAP_CONTENT, 
            0.25f)); 

Điều này có nghĩa sử dụng 100% chiều cao của cha mẹ nhưng 25% của cha mẹ chiều rộng .

+0

Tôi tin rằng đây phải là LinearLayout.LayoutParams và 0,25 phải hoạt động. – thomas88wp

+0

@ thomas88wp Rõ ràng là mã này yêu cầu nhập LinearLayout.LayoutParams. Ngoài ra, 0,25 và 0,25f là điều tương tự trong Java vì dấu thập phân. – Emmanuel

+4

xin lỗi để belabor điểm, nhưng trong java '0,25' là một đôi chữ, và '0,25f' là một chữ nổi. Nếu một tham số yêu cầu một float (như trong trường hợp này), nó sẽ không biên dịch nếu bạn vượt qua nó một đôi (mặc dù cách khác xung quanh không hoạt động, như phao có thể được ngầm đúc đến một đôi nhưng không ngược lại). Để minh họa, hãy thử biên dịch 'public void foo (thanh float) {foo (0.5); } '- nó sẽ không hoạt động cho đến khi bạn thêm 'f'. – thomas88wp

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