2013-05-08 55 views
0

我有這個代碼,需要以下位置的圖像。但是,因爲我使用了相對佈局,&圖像沒有在所有設備中正確對齊,有些圖像重疊。 編輯:主要要求:在我的要求中,我想點擊綠色帽子。所以,如果我按下紅色帽子,該ImageView將會消失。因此,剩下的兩個圖像必須位於同一個位置。因此,如果我使用layout_weight,它將剩餘的兩個圖像覆蓋到剩餘的空間。 我怎樣才能得到這個要求的最佳輸出。在佈局上顯示圖像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#657383" 
android:orientation="horizontal" > 



<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:contentDescription="@string/app_name" /> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_centerInParent="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:contentDescription="@string/app_name" /> 

<ImageView 
    android:id="@+id/imageView3" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:contentDescription="@string/app_name" /> 


Model Image

謝謝。

回答

1

您可以使用LinearLayoutandroid:layout_weight屬性。爲所有ImageView設置相同的layout_weight屬性。通過這個,所有的ImageView將會佔用相同的空間。還可以使用android:scaleType="fitXY"

例如,如果我考慮編輯代碼..。(只是書面方式這裏,所以可能會有一些小錯誤)

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


    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:scaleType="fitXY" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:contentDescription="@string/app_name" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_centerInParent="true" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:scaleType="fitXY" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:contentDescription="@string/app_name" /> 

<ImageView 
    android:id="@+id/imageView3" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true"  
    android:layout_width="0dip" 
    android:layout_weight="1" 
    android:scaleType="fitXY" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:contentDescription="@string/app_name" /> 
+1

水平LinearLayour與layout_widht =「0dip」。 +1 – Blackbelt 2013-05-08 07:22:19

+0

請檢查編輯後的問題。 – sai 2013-05-08 07:43:47

+0

只需使用android:visibility =「gone」,當你需要使其不可見時 – stinepike 2013-05-08 08:16:36

0

試試這個

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#657383" 
    android:orientation="horizontal" > 
    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:layout_weight="1" 
     android:src="@drawable/ic_launcher" 
     android:contentDescription="@string/app_name" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:layout_weight="1" 
      android:src="@drawable/ic_launcher" 
     android:contentDescription="@string/app_name" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:layout_weight="1" 
     android:src="@drawable/ic_launcher" 
     android:contentDescription="@string/app_name" /> 

    </LinearLayout> 
+0

請檢查編輯後的問題。 – sai 2013-05-08 07:44:16

+0

使用View.INVISIBLE而不是View.GONE – 2013-05-08 08:21:00

1

您可以使用

android:layout_toRightOf="" // specify id of image-view 
android:layout_toLeftOf="" // specify id of image-view 

在值字段中您需要指定圖像視圖的編號

0

RelativeLayout是多餘的。你可以只設置線性佈局,以中心:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:background="#657383" 
    android:orientation="horizontal" > 
    <ImageView 
     ... 

注意多餘的線條android:layout_gravity="center"android:gravity="center"

0

如果你想圖像不重疊,你必須用更小的屏幕上使用的設備較小的圖像。您可以使用drawable-large- * dpi和xlarge- dpi或drawable-sw ** dp- * dpi文件夾查看更大的圖像,並使用drawable- * dpi查找手機。

Btw。你不需要LinearLayout來包裝一切。