1

我建立了一個Android應用程序中,我有3個ImageView正穿過一個LinearLayout橫放ImageViews,他們被安置了android:layout_width="0dp"android:layout_weight="1",使得他們有一個即使在佈局蔓延。我怎樣才能均勻地間隔開我的RelativeLayout的

現在我要切換到使用RelativeLayout(因爲我想重疊另一個形象,不能用一個LinearLayout來完成),所以我要開始複製具有相同的效果3 ImageView小號均勻分佈/跨父級佈局縮放,但我不知道如何實現這一點。

我覺得我需要利用android:scaleType的......也許居中裁剪:

縮放圖像均勻(保持圖像的長寬比),這樣的兩個維度(寬度和高度)圖像將等於或大於視圖的相應尺寸(減去填充)。

這聽起來不錯,但我似乎無法得到它的工作吧... 任何關於我怎麼會做到這一點甚至可以跨越我的RelativeLayout傳播ImageView思想?

的代碼片斷現在:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 
    <ImageView 
     android:id="@+id/dragcircle" 
     android:layout_width="wrap_content" 
     android:tag="circle" 
     android:layout_alignParentLeft="true" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="false" 
     android:scaleType="centerCrop" 
     android:src="@drawable/circle" /> 
    <ImageView 
     android:id="@+id/dragsquare" 
     android:layout_width="wrap_content" 
     android:tag="square" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/dragcircle" 
     android:adjustViewBounds="false" 
     android:src="@drawable/square" /> 
    <ImageView 
     android:id="@+id/dragtriangle" 
     android:layout_width="wrap_content" 
     android:tag="triangle" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/dragsquare" 
     android:adjustViewBounds="false" 
     android:src="@drawable/triangle" /> 


注:我無法找到相同的約束,因爲這對一個SO一個問題。有一些問題,如:

Android: how evenly space components within RelativeLayout?android RelativeLayout, equal spacing?

但如果你檢查出的細節,你會發現他們是誰,有沒有考慮LinearLayout爲相等的間距和切換的選項的人佈局類型最終成爲解決方案。我有,我用它,但它沒有做工作適合我,因爲我需要重疊的圖像:

注意的例子,我有3個ImageView s的基本形狀,但我也有一個4 ImageView(它開始隱藏),它與中間的一個重疊。這就是爲什麼我必須使用RelativeLayout

enter image description here

+0

爲什麼不使用relativeLayout,並在其內部使用LinearLayout(爲了使用'layout_weigt')選項,然後可以在父佈局(這是relativeLayout)中重疊新的imageview – Houcine

+0

該解決方案不會使重疊的圖像與它將要重疊的圖像成比例地縮放。 – goto10

回答

2

我想你會想回到你原來的LinearLayout,以滿足您的所有需求在這裏。

如果您的第四張圖片的大小必須與您現有的圖片中的一張匹配,那麼您要創建一個資源,該圖片需要疊加或替換您的中心ImageView使用包含ImageView的RelativeLayout或FrameLayout。當您需要添加第四個圖像時,將其添加到該佈局。

喜歡的東西:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <ImageView 
     android:id="@+id/dragcircle" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:tag="circle" 
     android:layout_height="wrap_content" 
     android:scaleType="centerCrop" 
     android:src="@drawable/circle" /> 
    <FrameLayout 
     android:id="@+id/centerimagewrapper" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" > 
     <ImageView 
      android:id="@+id/dragsquare" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:tag="square" 
      android:src="@drawable/square" /> 
     <ImageView 
      android:id="@+id/arrow" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/arrow" 
      android:visibility="invisible" /> 
    </FrameLayout> 
    <ImageView 
     android:id="@+id/dragtriangle" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:tag="triangle" 
     android:layout_height="wrap_content" 
     android:src="@drawable/triangle" /> 
+0

至於這兩個圖像的「複合」......不知道你的意思,但他們需要是單獨的圖像。藍色的圓圈將被「拖拽」,然後綠色的箭頭將變得可見並聚焦並且可以與之交互。我沒有考慮將中間的'ImageView'換成包含我的兩個'ImageView'的RelativeLayout' ......這是容器中的很多容器,但這可能是正常的。我會試一試。 – Mike

+0

您也可以編寫自定義視圖或佈局,以明確定位需要它們的地方。這可能比僅僅爲了避免一個額外的視圖值得更多的努力,但如果你發現層次結構變得更加複雜,這是一個不錯的選擇。 – goto10

+0

太棒了,這是爲我做的!唯一的區別是我的第二個嵌入式佈局是一個'RelativeLayout',我對「FrameLayout」還不是很熟悉,所以我想堅持我現在知道的。非常感謝。 – Mike

0

試試這個

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/dragcircle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/circle" 
      android:tag="circle" /> 

     <ImageView 
      android:id="@+id/dragtriangle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/triangle" 
      android:scaleType="fitXY" 
      android:tag="triangle" /> 

     <ImageView 
      android:id="@+id/dragsquare" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@+id/dragtriangle" 
      android:layout_toRightOf="@id/dragcircle" 
      android:src="@drawable/square" 
      android:scaleType="fitXY" 
      android:tag="square" /> 

</RelativeLayout> 
+0

感謝您的輸入,但它不起作用。事情是圖像很大,如果你把它們放在一個相鄰的肖像智能手機顯示屏上(它們的大小),它們只適合三種形狀中的兩種。這是通過'layout_weight'在'LinearLayout'中解決的,其中每個都被縮放到整個屏幕尺寸的33%,從而使它們很好地吻合。只是正確定位它們是不夠的,不知何故它們也需要擴展。 – Mike

+0

嘗試我編輯的答案 – Manishika

+1

編輯的答案仍然不能確保統一縮放。它們將被統一定位(左,中,右),但會以其原始尺寸進行渲染而不是縮放,以使每個都佔據1/3的寬度。 – goto10

1

可以隱藏你想將現有的圖像,並保留原來的LinearLayout實現這一目標的圖標。你的LinearLayout的每個組件是一個自定義佈局(充氣):

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
> 
<ImageView 
    android:id="@+id/img" 
    android:layout_width="100dip" 
    android:layout_height="100dip" 
    android:layout_centerInParent="true" 
    android:scaleType="fitXY" 
    android:background="@android:color/transparent" 
    android:src="img1_src" 
/> 

<ImageView 
    android:id="@+id/imgOverlap" 
    android:layout_width="50dip" 
    android:layout_height="50dip" 
    android:layout_centerInParent="true" 
    android:scaleType="fitXY" 
    android:adjustViewBounds="true" 
    android:background="@android:color/transparent" 
    android:src="img2_src" 
    android:visibility="gone" 
/> 
</RelativeLayout> 

這似乎不可能在一個RelativeLayout的使用「layout_weight」。 你也可以考慮一個GridView並設置其列數; GridView的每一項都是上面的膨脹佈局。

+0

這個建議的解決方案几乎沒有問題 - RelativeLayout上沒有權重來確保均勻間隔/縮放。此外,圖像都是固定大小的,因此它們不符合縮放視圖的要求。這些問題在我早些時候的回答中已經提到 – goto10

1

你也可以用編程方式做,並告訴它們是屏幕寬度的33%。如果要實現此目的,請查看DisplayMetrics和每個ImageView的屬性。

+0

這是一個整潔的提示。對於我目前的目的,我將使用嵌入式佈局,但我會牢記這一點。我通常喜歡用編程方式做事情。 – Mike