Android组件在一行列表中

时间:2017-04-03 19:49:47

标签: android android-layout user-interface

我是Android的新手,我想制作我的Android应用程序的UI设计,这只是一个音乐播放器。

徽标,文字,播放图像和持续时间是4个组件,我想将它们放在一行中。

当我尝试放置徽标图片时,它不会显示它。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:background="@drawable/white_selector"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:paddingRight="16dp"
    android:paddingLeft="16dp">

<ImageView
    android:id="@+id/logo"
    android:layout_width="40dp"
    android:layout_height="30dp"
    android:layout_toLeftOf="@+id/name"
    android:layout_toStartOf="@+id/badges"
    android:src="@drawable/ic_launcher"
    android:contentDescription="@string/audio_play"/>

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toRightOf="@+id/badges"
    android:layout_toLeftOf="@+id/badges"
    android:layout_toStartOf="@+id/badges"
    android:textColor="@color/text_color"
    tools:text="Martin Garrix - Animals"/>

<LinearLayout
    android:id="@+id/badges"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dp"
    android:layout_marginStart="4dp"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">

<ImageView
    android:id="@+id/play"
    android:layout_width="40dp"
    android:layout_height="30dp"
    android:src="@drawable/ic_play"
    android:contentDescription="@string/audio_play"/>

<TextView
    android:id="@+id/duration"
    android:layout_width="40dp"
    android:layout_height="30dp"
    android:layout_marginLeft="5dp"
    android:layout_marginStart="5dp"
    android:textColor="@color/asphalt"
    android:textSize="15sp"
    android:gravity="center"
    tools:text="3:05"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

布局有点乱,我认为它可以重构(LinearLayout可以删除)。无论如何,唯一看似错误的是名称 TextView定位,如果android:allignParentLeft设置为true,则不应设置android:layout_toRightOf,它也是应位于徽标 ImageView的左侧,而不是徽章 LinearLayout,我是对的吗?

结果应该是

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/logo"
    android:layout_toStartOf="@+id/logo"
    android:textColor="@color/text_color"
    tools:text="Martin Garrix - Animals"/>

答案 1 :(得分:0)

按以下方式更新XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:background="@drawable/white_selector"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:paddingRight="16dp"
    android:paddingLeft="16dp">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="40dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:src="@mipmap/ic_launcher"
        android:contentDescription="@string/audio_play"/>


    <LinearLayout
        android:id="@+id/badges"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <ImageView
            android:id="@+id/play"
            android:layout_width="40dp"
            android:layout_height="30dp"
            android:src="@drawable/ic_play"
            android:contentDescription="@string/audio_play"/>

        <TextView
            android:id="@+id/duration"
            android:layout_width="40dp"
            android:layout_height="30dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="15sp"
            android:gravity="center"
            tools:text="3:05"/>
    </LinearLayout>

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/logo"
        android:layout_toEndOf="@id/logo"
        android:layout_toLeftOf="@+id/badges"
        android:layout_toStartOf="@+id/badges"
        android:textColor="@color/colorAccent"
        tools:text="Martin Garrix - Animals"/>
</RelativeLayout>

<强>输出:

enter image description here