ImageView不是居中对齐

时间:2014-05-22 06:49:50

标签: android android-layout relativelayout vertical-alignment

我正试图在RelativeLayout中垂直居中对齐ImageView但无法这样做。这似乎很简单,但我无法解决问题。这是我定义的xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="63dp"
    android:background="#aa00ff">

    <ImageView
        android:id="@+id/user_dp"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="12dp"
        android:layout_marginLeft="12dp"
        android:background="#ffff00"/>


</RelativeLayout>

注意:此视图用于ListView行。

enter image description here

5 个答案:

答案 0 :(得分:5)

删除此

android:layout_alignParentLeft="true"

并试试这个

android:layout_centerInParent="true"

ImageView

答案 1 :(得分:2)

因为它是你正在谈论的listView(下次从一开始就提到它...)它改变了问题。你使用自定义适配器吗?

如果在你的getView中膨胀xml时你应该这样做:

view = LayoutInflater.from(context).inflate(R.layout.item_layout, viewGroup, false);

这可以确保你的布局参数保持不变 - 如果你将它作为父项赋予null,则完全忽略参数。

答案 2 :(得分:0)

试试这个,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="63dp"
    android:background="#aa00ff">

    <ImageView
        android:id="@+id/user_dp"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:background="#ffff00"/>


</RelativeLayout>

答案 3 :(得分:0)

Try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#aa00ff">

    <ImageView
        android:id="@+id/user_dp"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:background="#ffff00"/>


</RelativeLayout>

答案 4 :(得分:0)

// Try this way,hope this will help you to solve your problem...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="63dp"
    android:background="#aa00ff"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/user_dp"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_marginRight="12dp"
        android:layout_marginLeft="12dp"
        android:background="#ffff00"/>


</LinearLayout>
相关问题