【问题标题】:Custom row with circular ImageView and textview in androidandroid中带有圆形ImageView和textview的自定义行
【发布时间】:2017-09-27 14:09:19
【问题描述】:

我正在尝试为我的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。因此,我面临两个问题:

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

2) 如果布局有细微的变化,文本不会留在中心。

【问题讨论】:

    标签: android android-imageview


    【解决方案1】:

    在你的代码中试试这个。

    <?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>
    

    【讨论】:

    • 祝你好运。@Hiren
    • 布局底部的ImageView 在不可见的情况下占据了整个高度。该怎么办 ? @KeLiuyue
    • 如果你在你的布局中设置了`android:visibility="gone"`,它不会在你的布局中占用高度。如果您设置android:visibility="visible"android:visibility="invisible",它将占用高度。
    • 实际上,我在某个时间点以编程方式将其变为visible
    【解决方案2】:

    试试这个。您可以使用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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 您能否提供上述布局的实现,使用ConstraintLayout
      • 添加这个依赖 compile 'com.android.support.constraint:constraint-layout:1.0.2'
      【解决方案4】:

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

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

      【讨论】:

      • 我已经为多个屏幕创建了不同的dimen.xml。
      猜你喜欢
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      相关资源
      最近更新 更多