首页 > 解决方案 > 如何在android中的视图上重叠imageview

问题描述

我是Android的初学者。我想做这个。

在此处输入图像描述

但我不知道怎么做……我的是这个

在此处输入图像描述

我想ImageView在 line() 上重叠 bus( View)。

这是我的代码...

activity_main.xml

<?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="match_parent"
    android:layout_margin="30dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabLayout"/>

    <ImageView
        android:id="@+id/busImage"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_below="@+id/tabLayout"
        android:src="@drawable/ic_directions_bus_24dp"/>

</RelativeLayout>

activity_custom.xml

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="8">

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

            <View
                android:id="@+id/line1"
                android:layout_width="5dp"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_weight="1"
                android:background="@color/colorDS"/>

            <View
                android:id="@+id/line2"
                android:layout_width="5dp"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_weight="1"
                android:background="@color/colorDS"/>
        </RelativeLayout>

        <View
            android:id="@+id/view"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:layout_weight="6">

            <TextView
                android:id="@+id/stNm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"/>

            <TextView
                android:id="@+id/arsId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/stNm"
                android:textSize="13dp"/>

        </RelativeLayout>

    </LinearLayout>

标签: androidlayout

解决方案


您可以使用 RecyclerView 实现此布局。您需要将总线图像放在 RecyclerView 的每个项目中并相应地处理可见性。

这是 RecyclerView 的项目布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <View
        android:id="@+id/line1"
        android:layout_width="5dp"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="#ff0000"
        android:layout_marginStart="32dp"/>

    <TextView
        android:id="@+id/stNm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/line1"
        android:layout_marginStart="40dp"
        android:text="testzczczc"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/arsId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/line1"
        app:layout_constraintTop_toBottomOf="@id/stNm"
        android:layout_marginStart="40dp"
        android:text="tesdfsfsfsst"
        android:textSize="20dp"/>

    <ImageView
        android:id="@+id/busImage"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/bus"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="@id/line1"
        app:layout_constraintEnd_toEndOf="@id/line1"/>

</android.support.constraint.ConstraintLayout>

在这里你需要处理 busImage 的可见性。

在此处输入图像描述


推荐阅读