2015-06-19 33 views
0

如果我想在佈局中心有一張圖片,我可以輕鬆使用:「垂直居中」和「居中水平居中」。但現在我想要一張照片放在左側的中心。但我不想使用marginLeft = .. dp。我想在沒有DP的情況下工作。是否有像「垂直居中」等的可能性?這是帶有dp值的代碼。我可以用什麼來代替MarginLeft「38dp」?Eclipse Android佈局:沒有dp值?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
     <ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    android:layout_centerInParent="true" 
    android:layout_marginLeft="41dp" 
    android:src="@drawable/rosezwei" /> 

</RelativeLayout> 
+0

'android:layout_centerInParent =「true」'? –

+0

你的意思是像上面編輯過的帖子? – Jannis

+0

是的。這不行嗎? –

回答

0

使用

android:layout_gravity="left|center_vertical" 

會工作的LinearLayout視圖中。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 
    <ImageView 
android:id="@+id/imageView1" 
android:layout_width="150dp" 
android:layout_height="150dp" 
android:layout_gravity="left|center_vertical" 
android:src="@drawable/rosezwei" /> 

</LinearLayout > 

請注意,LinearLayout是水平的,所以視圖可以選擇其垂直位置。

+0

我通過代碼替換了「android:layout_centerInParent =」true「」和「android:layout_marginLeft =」41dp「,但它沒有幫助,或讓我錯誤? – Jannis

+0

那麼,這是解決方案,也許相對佈局不接受重力參數,你可以嘗試使用FrameLayout嗎? –

+0

沒有,這不起作用。中間的左側,但直接在邊緣 – Jannis

0

如果我理解正確的,你想在你的屏幕寬度的25%...圖像中心
唉Android有在XML沒有比例,要做到這一點,你必須使用一個LinearLayout分割屏幕

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".5" 
     android:layout_gravity="center" 
     android:src="@drawable/rosezwei" /> 
    <Space 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".5"/> 
</LinearLayout> 
+0

他顯示我在空間錯誤? – Jannis

+0

空間需要API等級14,但你可以把任何空的佈局,而不是... – Ilario