2015-06-19 64 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