问题描述:

once again I'm struggling with some layout stuff.

I have a CardView Layout with two TextViews and a SwitchCompat.

Right now the switch is left and the texts are right of it (vertically). I want this layout the opposite way, like for example in the Android Notification Settings of an app:

My problem is, that when I "turn my current layout around", the switch gets pushed out to the right when the textView gets too long.

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:paddingTop="20dp"

android:paddingBottom="20dp"

android:paddingRight="20dp"

android:paddingLeft="16dp"

android:id="@+id/relative">

<android.support.v7.widget.SwitchCompat

android:id="@+id/switch_card"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginRight="20dp" />

<TextView

android:id="@+id/textView_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/switch_card"

android:singleLine="false"

android:text="@string/title_card"

android:textColor="@color/text_black"

android:textSize="22dp" />

<TextView

android:id="@+id/textView_reminder"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/textView_title"

android:layout_toRightOf="@id/switch_card"

android:paddingTop="10dp"

android:singleLine="false"

android:text="@string/reminder_card"

android:textColor="@color/text_grey"

android:textSize="18dp" />

</RelativeLayout>

Can you please help me changing my layout that it fits the one of the screenshot? I just can't seem to get it right by myself.

网友答案:

try this : the trick to align the SwitchCompat to the righParent and put some margin to the TextView's you are using so they do not overlap the SwitchCompat

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:paddingRight="20dp"
        android:paddingLeft="16dp"
        android:id="@+id/relative">

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/switch_card"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp" />

        <TextView
            android:id="@+id/textView_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="40dp"
            android:singleLine="false"
            android:text="@string/title_card"
            android:textColor="@color/text_black"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/textView_reminder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView_title"
            android:layout_marginRight="40dp"
            android:paddingTop="10dp"
            android:singleLine="false"
            android:text="@string/reminder_card"
            android:textColor="@color/text_grey"
            android:textSize="18dp" />

    </RelativeLayout>

Hope this was helpful

Good luck !

相关阅读:
Top