有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java RelataiveLayout定位

我正在尝试使用Android XML布局模板实现布局:

Layout Diagram

我现在有XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginBottom="12dp"
    安卓:layout_marginTop="0dp"
    安卓:background="@color/grey"
    安卓:clickable="true"
    安卓:orientation="horizontal"
    安卓:padding="0dp">


    <RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
        xmlns:tools="http://schemas.安卓.com/tools"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_centerInParent="false"
        安卓:layout_centerVertical="true"
        安卓:layout_marginBottom="0dp"
        安卓:duplicateParentState="true"
        安卓:orientation="horizontal"
        安卓:gravity="center_horizontal"
        安卓:paddingTop="@dimen/schedule_row_paddingtop">


        <RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
            xmlns:tools="http://schemas.安卓.com/tools"
            安卓:layout_width="wrap_content"
            安卓:layout_height="@dimen/home_row_teamLogoHeight"
            安卓:layout_centerInParent="false"
            安卓:layout_centerVertical="true"
            安卓:layout_marginBottom="0dp"
            安卓:duplicateParentState="true"
            安卓:orientation="horizontal"
            安卓:gravity="center_horizontal"
            安卓:paddingTop="6dp">

            <ImageView
                安卓:id="@+id/teamALogo"
                安卓:layout_width="wrap_content"
                安卓:layout_height="match_parent"
                安卓:scaleType="fitCenter"
                安卓:src="@drawable/france_logo"
                安卓:visibility="visible" />
        </RelativeLayout>




        <TextView
            安卓:id="@+id/programmeSport"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:ellipsize="end"
            安卓:maxLines="1"
            安卓:text="sport"
            安卓:gravity="center_horizontal"
            安卓:textColor="@color/yellow"
            安卓:textAllCaps="true"
            安卓:textSize="@dimen/home_row_league_title"
            安卓:textStyle="bold" />


        <TextView
            安卓:id="@+id/programmeTitle"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_below="@+id/programmeSport"
            安卓:ellipsize="end"
            安卓:gravity="center_horizontal"
            安卓:maxLines="1"
            安卓:text="programme title"
            安卓:textColor="@color/white"
            安卓:layout_marginTop="-4dip"
            安卓:textSize="@dimen/home_row_programmeTitle"

            />



        <TextView
            安卓:id="@+id/programmeDay"
            安卓:layout_height="wrap_content"
            安卓:text="2:30 PM"
            安卓:textAllCaps="true"
            安卓:gravity="center_horizontal"
            安卓:layout_marginTop="-6dp"
            安卓:textColor="@color/green"
            安卓:textSize="@dimen/home_row_date"
            安卓:layout_below="@+id/programmeTitle"
            安卓:layout_width="match_parent"
            />


        <RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
            xmlns:tools="http://schemas.安卓.com/tools"
            安卓:layout_width="wrap_content"
            安卓:layout_height="@dimen/home_row_teamLogoHeight"
            安卓:layout_centerInParent="false"
            安卓:layout_centerVertical="true"
            安卓:layout_marginBottom="0dp"
            安卓:duplicateParentState="true"
            安卓:orientation="horizontal"
            安卓:gravity="right"
            安卓:paddingTop="6dp">

            <ImageView
                安卓:id="@+id/teamBLogo"
                安卓:layout_width="wrap_content"
                安卓:layout_height="wrap_content"
                安卓:scaleType="fitCenter"
                安卓:src="@drawable/italy_logo"
                安卓:layout_alignParentRight="true"
                安卓:gravity="center_horizontal"
                安卓:visibility="visible" />

        </RelativeLayout>



    </RelativeLayout>

</RelativeLayout>

目前它看起来不太好,因为存在一些重叠,在更大的屏幕上,“A队徽标”和“B队徽标”浮动到屏幕边缘(按计划浮动在文本的左侧和右侧),除了中间的文本之外,还留下了一个巨大的游戏:

Sample output of the issue in Android device

我需要的图像,浮动在左侧和右侧,以在同一位置垂直为每个夹具,并为他们与“队v队B”水平线

任何帮助都将不胜感激

谢谢


共 (1) 个答案

  1. # 1 楼答案

    您应该遵循ConstraintLayout或LinearLayout以实现所需的输出。 你可以像下面这样做

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView
        android:id="@+id/teamALogo"
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_team_a_logo"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintLeft_toRightOf="@id/teamALogo"
        app:layout_constraintRight_toLeftOf="@id/teamBLogo"
        app:layout_constraintTop_toTopOf="@id/teamBLogo"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toBottomOf="@id/teamBLogo"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="League"
            android:textSize="17sp"
            android:textColor="@color/black"
            android:gravity="center"
            android:textStyle="bold"
            android:layout_marginTop="5dp"
            android:padding="5dp"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Team A"
                android:textSize="17sp"
                android:textColor="@color/black"
                android:gravity="start"
                android:textStyle="normal"
                android:layout_marginTop="5dp"
                android:padding="5dp"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_weight=".5"
                android:layout_height="wrap_content"
                android:text="Vs"
                android:textSize="17sp"
                android:textColor="@color/black"
                android:gravity="start"
                android:textStyle="normal"
                android:layout_marginTop="5dp"
                android:padding="5dp"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Team B"
                android:textSize="17sp"
                android:textColor="@color/black"
                android:gravity="start"
                android:textStyle="normal"
                android:layout_marginTop="5dp"
                android:padding="5dp"
                />
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Date"
            android:textSize="17sp"
            android:textColor="@color/black"
            android:gravity="center"
            android:textStyle="bold"
            android:layout_marginTop="5dp"
            android:padding="5dp"
            />
    </LinearLayout>
    <ImageView
        android:id="@+id/teamBLogo"
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_team_b_logo"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>