簡體   English   中英

RelativeLayout 在 ConstraintLayout 中的位置不理想

[英]RelativeLayout doesn't locate well in ConstraintLayout

我認為我所做的一切都是正確的,但 ConstraintLayout 中的 RelativeLayout 並沒有按照我的命令執行。 當我在 RelativeLayout 下添加一些東西時,它應該更小並為其他項目騰出空間,但我做什么並不重要,它總是將它們推出屏幕。

在此處輸入圖像描述

左右箭頭應位於 RelativeLayout 下方,adView 應位於箭頭下方。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:id="@+id/relativeLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingTop="25dp"
    android:paddingBottom="25dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">


    <RelativeLayout
        android:id="@+id/container_main"
        android:paddingBottom="16dp"
        android:background="@drawable/card_bg"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">
        <TextView
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/title_text_color"
            android:id="@+id/activity_main_text_view_id"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:layout_marginTop="20dp"
            android:text="simple Title Text"/>

        <TextView
            android:padding="10dp"
            android:gravity="center"
            android:textColor="#000000"
            android:lineSpacingExtra="8dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/factTextView"
            android:textSize="18dp"
            android:layout_alignStart="@id/activity_main_text_view_id"
            android:layout_alignLeft="@id/activity_main_text_view_id"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="8dp"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."/>

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:id="@+id/activity_main_did_u_know"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="Category"
            android:layout_marginTop="20dp"/>
        <ImageView
            android:layout_marginLeft="25dp"
            android:id="@+id/activity_main_image_view"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:clickable="true"
            android:src="@drawable/ic_share_black_24dp" />

        <ImageView
            android:layout_marginRight="25dp"
            android:id="@+id/activity_main_fav_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:clickable="true"
            android:src="@drawable/ic_favorite_border_black_24dp"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/LeftRight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/container_main"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        >
        <ImageView
            android:layout_marginLeft="25dp"
            android:id="@+id/activity_main_left_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:clickable="true"
            app:layout_constraintRight_toLeftOf="@+id/activity_main_right_button"
            android:src="@drawable/ic_left" />

        <ImageView
            android:id="@+id/activity_main_right_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="25dp"
            android:clickable="true"
            android:src="@drawable/ic_right"
            app:layout_constraintLeft_toRightOf="@+id/activity_main_left_button" />

    </RelativeLayout>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintTop_toBottomOf="@id/LeftRight"
        ads:layout_constraintRight_toRightOf="parent"
        ads:layout_constraintLeft_toLeftOf="parent"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</androidx.constraintlayout.widget.ConstraintLayout>

我願意接受任何建議。 謝謝。

我更喜歡在xml中使用整個Constraint Layout xml下方結帳,希望與您想要的設計相同。

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="16dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@drawable/card_bg"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/activity_main_text_view_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="simple Title Text"
            android:textColor="@color/title_text_color"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/activity_main_did_u_know"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="Category"
            android:textColor="#000000"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/factTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:lineSpacingExtra="8dp"
            android:padding="10dp"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
            android:textColor="#000000"
            android:textSize="18dp"
            app:layout_constraintBottom_toTopOf="@id/activity_main_image_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/activity_main_did_u_know" />

        <ImageView
            android:id="@+id/activity_main_image_view"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_margin="16dp"
            android:clickable="true"
            android:src="@drawable/ic_share_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/factTextView" />

        <ImageView
            android:id="@+id/activity_main_fav_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_margin="16dp"
            android:clickable="true"
            android:src="@drawable/ic_favorite_border_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/factTextView" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/LeftRight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/container_main">

        <ImageView
            android:id="@+id/activity_main_left_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_margin="25dp"
            android:clickable="true"
            android:src="@drawable/ic_left"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/activity_main_right_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_margin="25dp"
            android:clickable="true"
            android:src="@drawable/ic_right"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/activity_main_left_button"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintLeft_toLeftOf="parent"
        ads:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

有關更多信息,請參見下面的視圖。

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM