2010-04-06 47 views

Trả lời

38

ImageView của bạn có thuộc tính wrap_content. Tôi sẽ nghĩ rằng Hình ảnh được căn giữa bên trong hình ảnh ảo nhưng chính bản thân hình ảnh đó không nằm ở giữa chế độ xem chính. Nếu bạn chỉ có số lần xem trên màn hình, hãy thử match_parent thay vì wrap_content. Nếu bạn có nhiều hơn thì một chế độ xem trong bố cục bạn phải căn giữa chế độ xem hình ảnh.

+0

tôi có nhiều hơn một điểm trong cách bố trí. Ý bạn chính xác là gì khi "căn giữa chế độ xem hình ảnh"? – Yang

+0

Bản thân ImageView phải nằm trong thành phần bố cục. Thành phần bố cục xung quanh này có thể sắp xếp các phần tử con ... Nếu bạn có thể đăng toàn bộ tệp bố cục của mình, tôi có thể cố gắng cung cấp cho bạn một số gợi ý chính xác hơn. – Janusz

+5

Yang - Tôi nghĩ rằng vấn đề của bạn là bố mẹ là bố cục tương đối chứ không phải bố cục tuyến tính. Nếu đúng như vậy, hãy thử android: layout_centerHorizontal = "true" thay vì layout_gravity. –

29

Bạn có thể tập trung các ImageView bản thân bằng cách sử dụng:

android:layout_centerVertical="true" hoặc android:layout_centerHorizontal="true" Đối dọc hoặc ngang. Hoặc để tập trung bên phụ huynh:

android:layout_centerInParent="true"

Nhưng có vẻ như bạn đang cố gắng để tập trung các nguồn hình ảnh thực tế bên trong ImageView chính nó, mà tôi không chắc chắn về.

+13

lưu ý: điều này là đúng khi phụ huynh là RelativeLayout –

+0

Làm thế nào để đi về nó khi cha mẹ là một LinearLayout? – VikramV

+1

OK! Tôi đoán nếu cha mẹ của bạn là một LinearLayout, bạn có thể về thiết lập một thuộc tính trong đó là con ImageView - android: layout_gravity = "center". Điều này làm việc cho tôi. Cảm ơn. – VikramV

64
<ImageView 
android:id="@+id/img" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
/>; 
+15

lưu ý: điều này là đúng khi cha mẹ là một LinearLayout hoặc tương tự –

+32

Nếu cha mẹ là một 'RelativeLayout', sử dụng' android: layout_centerHorizontal = "true" ' –

+0

Giải pháp này hoạt động ngay cả khi Android Studio không hiển thị thuộc tính này. – CoolMind

1

Sử dụng "fill_parent" một mình đối với layout_width sẽ làm các trick:

<ImageView 
    android:id="@+id/image" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="6dip" 
    android:background="#0000" 
    android:src="@drawable/icon1" /> 
9

này làm việc cho tôi khi sắp xếp xem hình ảnh trong LinearLayout.

<ImageView android:id="@android:id/empty" 
    android:src="@drawable/find_icon" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:scaleType="center" 
    /> 
0

Sử dụng thuộc tính này: android: layout_centerHorizontal = "true"

1

Hãy thử:

android:layout_height="wrap_content" 
android:scaleType="fitStart" 

vào hình ảnh trong RelativeLayout

2

Đối với tôi android:gravity="center" đã làm các trick trong phần tử bố cục mẹ.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/fullImageView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:contentDescription="@string/fullImageView" 
     android:layout_gravity="center" /> 

</LinearLayout> 
-1

Đây là mã trong xml cách căn giữa ImageView, tôi đã sử dụng "layout_centerHorizontal".

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_centerHorizontal="true" 
    > 
    <ImageView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/img2" 
    /> 
    <ImageView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/img1" 
     /> 
</LinearLayout> 

hoặc ví dụ khác này ...

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="center_horizontal" 
    > 
    <ImageView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/img2" 
    /> 
    <ImageView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/img1" 
     /> 
</LinearLayout> 
+0

Nếu đây là câu trả lời, vui lòng thêm một số giải thích. Nếu nó được dự định là một câu hỏi, hãy hỏi một câu hỏi mới. –

6

Hãy thử mã này:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center_horizontal"> 

      <ImageView 
       android:id="@+id/imgBusiness" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:src="@drawable/back_detail" /> 
</LinearLayout> 
+0

Vui lòng cung cấp một số giải thích văn bản. –

+0

Nó hoạt động cho tôi, cảm ơn bạn :) –

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