2017-09-27 96 views
0

我想为我的RecyclerView创建自定义行。这里是我的自定义行布局:自定义行与循环ImageView和textview在Android

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

这是它的样子:

enter image description here

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

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

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

回答

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> 

enter image description here

+0

它解决了。谢谢:) – XoXo

+0

祝你好运。@ Hiren – KeLiuyue

+0

位于布局底部的'ImageView'处于不可见状态,占据了完整的高度。该怎么办 ? @KeLiuyue – XoXo

0

使用约束布局它可以帮助您只需拖动像你想设定图像和下降及补充约束

+0

可以提供使用'ConstraintLayout'上述布局的实现。 – XoXo

+0

添加此依赖项compile'c​​om.android.support.constraint:constraint-layout:1.0.2' –

0

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

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

+0

我已经为多个屏幕创建了不同的dimen.xml。 – XoXo

+0

但在xml文件中,我认为你硬编码的值。 – user2851150

1

尝试。您可以使用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> 
相关问题