RelativeLayout在ConstraintLayout中定位不好。

huangapple go评论73阅读模式
英文:

RelativeLayout doesn't locate well in ConstraintLayout

问题

<?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">
        
        <!-- ... Your TextView and other views within this RelativeLayout ... -->

    </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">

        <!-- ... Your ImageView views within this RelativeLayout ... -->

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

Please note that I have only provided the translated XML code as requested. If you have any questions or need further assistance, feel free to ask.

英文:

I think I did everything true but the RelativeLayout in ConstraintLayout don't do what i command. When i add something under the RelativeLayout it supposed to be smaller and make room to other items but it doesn't matter what i do, it always push them out of the screen.

RelativeLayout在ConstraintLayout中定位不好。

The left and right arrows should be below of RelativeLayout and adView should be below of arrows.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/relativeLayout&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:paddingTop=&quot;25dp&quot;
android:paddingBottom=&quot;25dp&quot;
android:paddingLeft=&quot;10dp&quot;
android:paddingRight=&quot;10dp&quot;&gt;
&lt;RelativeLayout
android:id=&quot;@+id/container_main&quot;
android:paddingBottom=&quot;16dp&quot;
android:background=&quot;@drawable/card_bg&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_margin=&quot;20dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
&lt;TextView
android:textStyle=&quot;bold&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:textColor=&quot;@color/title_text_color&quot;
android:id=&quot;@+id/activity_main_text_view_id&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_alignParentLeft=&quot;true&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_marginLeft=&quot;40dp&quot;
android:layout_marginStart=&quot;40dp&quot;
android:layout_marginTop=&quot;20dp&quot;
android:text=&quot;simple Title Text&quot;/&gt;
&lt;TextView
android:padding=&quot;10dp&quot;
android:gravity=&quot;center&quot;
android:textColor=&quot;#000000&quot;
android:lineSpacingExtra=&quot;8dp&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/factTextView&quot;
android:textSize=&quot;18dp&quot;
android:layout_alignStart=&quot;@id/activity_main_text_view_id&quot;
android:layout_alignLeft=&quot;@id/activity_main_text_view_id&quot;
android:layout_alignParentLeft=&quot;true&quot;
android:layout_alignParentRight=&quot;true&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:layout_marginTop=&quot;8dp&quot;
android:text=&quot;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;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.&quot;/&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:textColor=&quot;#000000&quot;
android:id=&quot;@+id/activity_main_did_u_know&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:layout_alignParentRight=&quot;true&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:layout_marginRight=&quot;16dp&quot;
android:text=&quot;Category&quot;
android:layout_marginTop=&quot;20dp&quot;/&gt;
&lt;ImageView
android:layout_marginLeft=&quot;25dp&quot;
android:id=&quot;@+id/activity_main_image_view&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:clickable=&quot;true&quot;
android:src=&quot;@drawable/ic_share_black_24dp&quot; /&gt;
&lt;ImageView
android:layout_marginRight=&quot;25dp&quot;
android:id=&quot;@+id/activity_main_fav_button&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_alignParentRight=&quot;true&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:clickable=&quot;true&quot;
android:src=&quot;@drawable/ic_favorite_border_black_24dp&quot;/&gt;
&lt;/RelativeLayout&gt;
&lt;RelativeLayout
android:id=&quot;@+id/LeftRight&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/container_main&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/adView&quot;
&gt;
&lt;ImageView
android:layout_marginLeft=&quot;25dp&quot;
android:id=&quot;@+id/activity_main_left_button&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:clickable=&quot;true&quot;
app:layout_constraintRight_toLeftOf=&quot;@+id/activity_main_right_button&quot;
android:src=&quot;@drawable/ic_left&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/activity_main_right_button&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_alignParentRight=&quot;true&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:layout_marginRight=&quot;25dp&quot;
android:clickable=&quot;true&quot;
android:src=&quot;@drawable/ic_right&quot;
app:layout_constraintLeft_toRightOf=&quot;@+id/activity_main_left_button&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;com.google.android.gms.ads.AdView
xmlns:ads=&quot;http://schemas.android.com/apk/res-auto&quot;
android:id=&quot;@+id/adView&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot;
ads:layout_constraintBottom_toBottomOf=&quot;parent&quot;
ads:layout_constraintTop_toBottomOf=&quot;@id/LeftRight&quot;
ads:layout_constraintRight_toRightOf=&quot;parent&quot;
ads:layout_constraintLeft_toLeftOf=&quot;parent&quot;
ads:adSize=&quot;BANNER&quot;
ads:adUnitId=&quot;ca-app-pub-3940256099942544/6300978111&quot;&gt;
&lt;/com.google.android.gms.ads.AdView&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

I am open the any kind of advice. Thanks.

答案1

得分: 1

<?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..."
            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>
英文:

I prefer to use whole Constraint Layout in your xml. Checkout below xml, hope the same what you want to design.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:padding=&quot;16dp&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:id=&quot;@+id/container_main&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;16dp&quot;
android:background=&quot;@drawable/card_bg&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;TextView
android:id=&quot;@+id/activity_main_text_view_id&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;16dp&quot;
android:text=&quot;simple Title Text&quot;
android:textColor=&quot;@color/title_text_color&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/activity_main_did_u_know&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;16dp&quot;
android:text=&quot;Category&quot;
android:textColor=&quot;#000000&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/factTextView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;8dp&quot;
android:lineSpacingExtra=&quot;8dp&quot;
android:padding=&quot;10dp&quot;
android:text=&quot;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;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.&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;18dp&quot;
app:layout_constraintBottom_toTopOf=&quot;@id/activity_main_image_view&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/activity_main_did_u_know&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/activity_main_image_view&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_margin=&quot;16dp&quot;
android:clickable=&quot;true&quot;
android:src=&quot;@drawable/ic_share_black_24dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/factTextView&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/activity_main_fav_button&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_margin=&quot;16dp&quot;
android:clickable=&quot;true&quot;
android:src=&quot;@drawable/ic_favorite_border_black_24dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/factTextView&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:id=&quot;@+id/LeftRight&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintBottom_toTopOf=&quot;@id/adView&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/container_main&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/activity_main_left_button&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_margin=&quot;25dp&quot;
android:clickable=&quot;true&quot;
android:src=&quot;@drawable/ic_left&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/activity_main_right_button&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_margin=&quot;25dp&quot;
android:clickable=&quot;true&quot;
android:src=&quot;@drawable/ic_right&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintLeft_toRightOf=&quot;@+id/activity_main_left_button&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;com.google.android.gms.ads.AdView xmlns:ads=&quot;http://schemas.android.com/apk/res-auto&quot;
android:id=&quot;@+id/adView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot;
ads:adSize=&quot;BANNER&quot;
ads:adUnitId=&quot;ca-app-pub-3940256099942544/6300978111&quot;
ads:layout_constraintBottom_toBottomOf=&quot;parent&quot;
ads:layout_constraintLeft_toLeftOf=&quot;parent&quot;
ads:layout_constraintRight_toRightOf=&quot;parent&quot;/&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

For more see below view .

RelativeLayout在ConstraintLayout中定位不好。

huangapple
  • 本文由 发表于 2020年5月31日 06:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/62109504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定