android 中带有圆形 ImageView 和 textview 的自定义行

Custom row with circular ImageView and textview in android

我正在尝试为我的 RecyclerView 创建一个自定义行。这是我的自定义行布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:layout_margin="5dp"
    android:background="#00000000">

    <FrameLayout
        android:id="@+id/customLevelLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/levelImage"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:layout_above="@+id/gameText"
                android:layout_centerInParent="true"
                android:scaleType="fitXY"
                android:src="@drawable/circular_background" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="1"
                android:textColor="#fff"
                android:textSize="45sp" />

            <TextView
                android:id="@+id/gameText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:textColor="#000"
                tools:text="Tracing" />
        </RelativeLayout>

        <ProgressBar
            android:id="@+id/gameProgressBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:indeterminate="true"
            android:padding="45dp"
            android:visibility="gone" />
    </FrameLayout>

    <ImageView
        android:id="@+id/lockImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/lock_background"
        android:padding="45dp"
        android:src="@drawable/lock"
        android:visibility="gone" />
</RelativeLayout>

这是它的样子:

问题是,我必须定义自定义行的预定义高度,即目前 170dp。因此,我面临 2 个问题:

1) 如果我在小屏幕或平板设备上打开应用程序。自定义行看起来很难看,即我松开了 ImageView.

的圆形

2) 如果布局稍有变化,文本不会保留在中心。

使用 constraintlayout 可以帮助您通过拖放和添加约束来设置您想要的图像

用于支持多屏。首先你必须通过这个 link enter link description here

一旦你解决了第一个问题,第二个问题也会得到解决

试试这个。您可以使用 Linearlayout 作为根而不是图像和文本使用 RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="#00000000"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/customLevelLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/levelImage"
                    android:layout_width="160dp"
                    android:layout_height="160dp"
                    android:layout_centerInParent="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/circular_background" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="1"
                    android:textColor="#fff"
                    android:textSize="45sp" />

            </RelativeLayout>

            <TextView
                android:id="@+id/gameText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_margin="10dp"
                android:gravity="center"
                android:textColor="#000"
                tools:text="Tracing" />
        </LinearLayout>

        <ProgressBar
            android:id="@+id/gameProgressBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:indeterminate="true"
            android:padding="45dp"
            android:visibility="gone" />
    </FrameLayout>

    <ImageView
        android:id="@+id/lockImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/lock_background"
        android:padding="45dp"
        android:src="@drawable/lock"
        android:visibility="gone" />
</LinearLayout>

在您的代码中试试这个。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#00000000">

<FrameLayout
    android:id="@+id/customLevelLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/levelImage"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:layout_centerInParent="true"
                android:scaleType="fitXY"
               android:src="@drawable/circular_background" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="1"
                android:textColor="#fff"
                android:textSize="45sp"/>
        </RelativeLayout>


        <TextView
            android:id="@+id/gameText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:textColor="#000"
            tools:text="Tracing"/>
    </LinearLayout>

    <ProgressBar
        android:id="@+id/gameProgressBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:indeterminate="true"
        android:padding="45dp"
        android:visibility="gone"/>
</FrameLayout>

<ImageView
    android:id="@+id/lockImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/lock_background"
    android:padding="45dp"
    android:src="@drawable/lock"
    android:visibility="gone" />
</RelativeLayout>