Android 将线性布局放置在包裹内容的中心

Android 将线性布局放置在包裹内容的中心,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我有一个listView,列表中每个项目的视图应该如下所示(背景图像和组名): 但是,当我将layout_centerInParent=“true”设置为linearLayout时,它会放置在屏幕的中心(而不是图像的中心)。如下所示: 还有我的xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android

我有一个listView,列表中每个项目的视图应该如下所示(背景图像和组名):

但是,当我将layout_centerInParent=“true”设置为linearLayout时,它会放置在屏幕的中心(而不是图像的中心)。如下所示:

还有我的xml:

<?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="wrap_content"
    android:background="#444">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/test"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/linear_category_name"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingRight="16dp"
        android:paddingLeft="16dp">
        <ir.dbasoft.test.myobject.MyTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:text="Gold"/>
    </LinearLayout>
</RelativeLayout>

如何将linearLayout放置在RelayvelLayout的中心???

试试这个

<?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="wrap_content"
android:background="#444">
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/test/>

    <ir.dbasoft.test.myobject.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:text="Gold"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

试试这个

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:layout_centerInParent="true"
        android:src="@drawable/test"/>

    <ir.dbasoft.test.myobject.MyTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:layout_centerInParent="true"
            android:text="Gold"/>

对于您的场景,我认为使用FrameLayout要容易得多

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/gold_image"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/white_border"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gold"
            android:textColor="@android:color/white"
            android:textSize="18sp"/>
    </LinearLayout>
</FrameLayout>

我已经测试过了,它看起来和你想要达到的目标一模一样