2013-02-02 18 views
1

我想將我的imageview放在中心上方一點。我已經使用了centerHorizo​​ntal =「true」和paddingBottom,但它被放置在絕對中心。 「填充」不在做這項工作。我該怎麼辦?如何從中心放置一個android組件偏移量?

<ImageView 
    android:id="@+id/imageview" 
    style="@style/myImageView" 
    android:layout_width="200dp" 
    android:layout_height="100dp" 
    android:layout_centerHorizontal="true" 
    android:paddingBottom="300dp" 
    android:src="@drawable/ic_launcher" /> 

回答

0

我希望你使用的是相對佈局而不是線性或絕對佈局。

+0

雅我使用相對佈局。 – Sourav301

1

嘗試佈局:margin_bottom。如果沒有得到你想要的。應用relativelayout和相同的bottom_margin。

+0

我正在使用相對佈局,但仍然bottom_margin不起作用。 – Sourav301

+0

好的。我可以再給你一個建議,而且確實有效。但它不是一個優化的方式。它擁有額外的內存。創建一個假的視圖,以可見性GONE的形式出現在佈局的中心。現在,在您看來,您想要稍微在中心放置android:layout_above =「@ + id/id_of_invisible view_which_is_in_center_we_just_created」。 使用這個技巧,你會得到你想要的。 –

+0

感謝您的想法。我在相對佈局中創建了一個框架佈局,定位了imageview,然後將重心設置爲中心。 – Sourav301

1

找不到確切的答案,但有另一種方法。我在相對佈局中創建了一個框架佈局,將imageview放置在其中,然後將重心設置爲中心。 如果有更好的解決方案,請發佈。 我的代碼:

Inside the relative layout-- 
<FrameLayout 
    android:layout_width="800dp" 
    android:layout_height="400dp" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    android:layout_marginRight="30dp" 
    android:layout_marginBottom="100dp" 

    > 
    <ImageView 
    android:id="@+id/imageview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:src="@drawable/ic_launcher" 
    style="@style/myImageView" /> 
</FrameLayout> 
相關問題