【问题标题】:Why adding the switch or checkbox button in Relative layout messes up the entire layout?为什么在相对布局中添加开关或复选框按钮会弄乱整个布局?
【发布时间】:2023-08-18 12:58:01
【问题描述】:

我想在片段布局中添加开关或复选框按钮。当我添加它时,它会加载布局直到复选框出现,它有点“破坏”布局,将它与之前的布局片段混合。

我尝试使用开关和复选框,但它搞砸了,使用另一个 textview 代替它工作正常。

这是一张带有 xml 开关的照片:Image with switch 如您所见,左边距被切掉了,在他的底部有以前布局的元素,它们是不可点击的。

这是一张带有 textview 而不是 switch 元素的照片:image without switch 在这里,布局完美。

这是 xml 布局,用于创建我想要的布局的结构并不简单。在布局文件的底部,你可以看到 switch 元素,我认为它破坏了整个布局。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:app="http://schemas.android.com/tools"
    android:background="#E8E8E8"
    app:ignore="NamespaceTypo">
    <!-- Here you put the rest of your current view-->

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"

        >

        <android.support.design.card.MaterialCardView
            android:id="@+id/acquisto_cardview_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardElevation="2dp"
            android:layout_margin="8dp"
            card_view:cardCornerRadius="5dp"
            card_view:strokeColor=" #696969"
            card_view:strokeWidth="1dp"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="4dp">

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_info_intestazione"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >

                    <TextView
                        android:id="@+id/acquisto_cardview_info_intestazione_text1"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/info_intestazione"
                        android:background="@drawable/textview_sottolineato"
                        style="@style/italictext_summary"
                        />

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_info_tot_articoli"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_below="@id/acquisto_cardview_info_intestazione"
                    >

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/acquisto_cardview_info_tot_articoli_txt_label"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/info_totale_articoli"
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentStart="true"
                            android:ellipsize="end"

                            />

                        <TextView
                            android:id="@+id/acquisto_cardview_info_tot_articoli_txt_prezzo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentEnd="true"
                            />

                    </RelativeLayout>


                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_info_punti_guadagnati"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_below="@id/acquisto_cardview_info_tot_articoli"
                    >

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/acquisto_cardview_info_punti_guadagnati_txt_label"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/info_punti_guagnati"
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentStart="true"
                            android:ellipsize="end"

                            />

                        <TextView
                            android:id="@+id/acquisto_cardview_info_punti_guadagnati_txt_prezzo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentEnd="true"
                            />

                    </RelativeLayout>


                </android.support.v7.widget.CardView>


            </RelativeLayout>

        </android.support.design.card.MaterialCardView>

        <android.support.design.card.MaterialCardView
            android:id="@+id/acquisto_cardview_impo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardElevation="2dp"
            android:layout_margin="8dp"
            card_view:cardCornerRadius="5dp"
            card_view:strokeColor=" #696969"
            card_view:strokeWidth="1dp"
            android:layout_below="@id/acquisto_cardview_info"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="4dp">

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_impo_intestazione"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >

                    <TextView
                        android:id="@+id/acquisto_cardview_impo_intestazione_text1"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/impo_intestazione"
                        android:background="@drawable/textview_sottolineato"
                        style="@style/italictext_summary"
                        />

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_impo_carta_addebitata"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_below="@id/acquisto_cardview_impo_intestazione"
                    >

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/acquisto_cardview_impo_carta_addebitata_txt_label"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/impo_carta_addebitata"
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentStart="true"
                            android:ellipsize="end"
                            android:layout_toStartOf="@id/acquisto_cardview_impo_carta_addebitata_txt_prezzo"

                            />

                        <TextView
                            android:id="@+id/acquisto_cardview_impo_carta_addebitata_txt_prezzo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Visa :x 5748"
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentEnd="true"

                            />

                    </RelativeLayout>


                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_impo_sconto_disponibile"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_below="@id/acquisto_cardview_impo_carta_addebitata"
                    >

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/acquisto_cardview_impo_sconto_disponibile_txt_label"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/impo_sconto_disponibile"
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentStart="true"
                            android:ellipsize="end"

                            />

                        <TextView
                            android:id="@+id/acquisto_cardview_impo_sconto_disponibile_txt_prezzo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentEnd="true"
                            />

                    </RelativeLayout>


                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_impo_applica_sconto"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_below="@id/acquisto_cardview_impo_sconto_disponibile"
                    >

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <!--inserire checkbox qui-->
                        <Switch
                            android:id="@+id/acquisto_cardview_impo_applica_sconto_switch"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/impo_checkbox_sconto"
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentStart="true"
                            android:ellipsize="end"
                            android:textOn="@string/on"
                            android:textOff="@string/off"
                            android:layoutDirection="rtl"
                            app:switchPadding="16dp"
                            />

                    </RelativeLayout>


                </android.support.v7.widget.CardView>


            </RelativeLayout>

        </android.support.design.card.MaterialCardView>

        <android.support.design.card.MaterialCardView
            android:id="@+id/acquisto_cardview_finale"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardElevation="2dp"
            android:layout_margin="8dp"
            card_view:cardCornerRadius="5dp"
            card_view:strokeColor=" #696969"
            card_view:strokeWidth="1dp"
            android:layout_below="@id/acquisto_cardview_impo"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="4dp">

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_finale_intestazione"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >

                    <TextView
                        android:id="@+id/acquisto_cardview_finale_intestazione_text1"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/finale_intestazione"
                        android:background="@drawable/textview_sottolineato"
                        style="@style/italictext_summary"
                        />

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_finale_totale_ordine"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_below="@id/acquisto_cardview_finale_intestazione"
                    >

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/acquisto_cardview_finale_totale_ordine_txt_label"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/finale_totale_ordine"
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentStart="true"
                            android:ellipsize="end"
                            android:layout_toStartOf="@id/acquisto_cardview_finale_totale_ordine_txt_prezzo"

                            />

                        <TextView
                            android:id="@+id/acquisto_cardview_finale_totale_ordine_txt_prezzo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            style="@style/acquisto_card_textview"
                            android:layout_alignParentEnd="true"
                            />



                    </RelativeLayout>


                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/acquisto_cardview_finale_bottone_acquisto"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_below="@id/acquisto_cardview_finale_totale_ordine"
                    >

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="TEST"
                            android:layout_centerHorizontal="true"
                            />



                    </RelativeLayout>


                </android.support.v7.widget.CardView>



            </RelativeLayout>

        </android.support.design.card.MaterialCardView>


    </RelativeLayout>

</ScrollView>

请帮帮我,我不知道这里发生了什么。

【问题讨论】:

    标签: android xml android-studio checkbox uiswitch


    【解决方案1】:

    为您的布局使用 ConstraintLayout 根视图组。 你会得到至少 2 件积极的东西:

    1. 您将删除“查看到查看”的副作用。
    2. 您将获得具有更好性能的扁平化布局。

    【讨论】:

    • 如果我用约束改变相对关系,我会失去所有下面的对齐,并且全部折叠到顶部。我还为滚动问题添加了一个滚动视图,当我滚动布局时完全加载,使它看起来像它假设的那样。我想现在是我的手机不适合这个
    最近更新 更多