android中具有圆形ImageView和textview的自定义行
我正在尝试为我的android中具有圆形ImageView和textview的自定义行,android,android-imageview,Android,Android Imageview,我正在尝试为我的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://schem
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) 如果布局稍有变化,文本不会保留在中间。使用约束布局可帮助您通过拖放和添加约束来设置所需的图像使用约束布局可帮助您通过拖放和添加来设置所需的图像约束以支持多屏幕。首先你必须通过这个链接
一旦您修复了第一个问题,第二个问题也将获得支持多屏幕的修复。首先你必须通过这个链接
一旦您修复了第一个问题,第二个问题也将得到修复试试这个。您可以使用
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>
试试这个。您可以使用
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>
在您的代码中尝试这一点
<?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>
能否使用
ConstraintLayout
提供上述布局的实现。添加此依赖项编译'com.android.support.constraint:constraint layout:1.0.2'能否使用ConstraintLayout
提供上述布局的实现。添加此依赖项编译“com.android.support.constraint:constraint layout:1.0.2”我已经为多个屏幕创建了不同的dimen.xml。我已经为多个屏幕创建了不同的dimen.xml。祝你好运。@HirenTheImageView
位于布局底部的不可见部分占据了整个高度。怎么办@keliuyue如果在布局中设置“android:visibility=“gone””,它将不会在布局中占据高度。如果你的设置是android:visibility=“visible”
或android:visibility=“invisible”
,它将占据高度。实际上,我通过编程在某个时间点将其设置为可见。祝你好运。@HirenTheImageView
位于布局底部,在不可见中占据了整个高度。怎么办@keliuyue如果在布局中设置“android:visibility=“gone””,它将不会在布局中占据高度。如果你的设置是android:visibility=“visible”
或android:visibility=“invisible”
,它将占据这个高度。实际上,我通过编程使它在某个时间点可见。