Reputation: 553
I want to center two switches as I did with the textviews but I've tried all the possible options without success.
This is a brief version of the code:
<LinearLayout [...]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="NOTIFICATIONS" />
<TextView [...] />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Switch
android:id="@+id/swnotif"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" />
<Switch [...] />
</LinearLayout>
</LinearLayout>
Upvotes: 0
Views: 1470
Reputation: 1368
try this i tested
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="NOTIFICATIONS" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="MUTE" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<Switch
android:id="@+id/swnotif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<Switch
android:id="@+id/swnotif1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
Upvotes: 1
Reputation: 24848
Set center_vertical gravity to TextView and Switch parent LinearLayout :
android:gravity="center_vertical"
Upvotes: 0
Reputation: 2159
The problem is that the gravity
attribute doesn't seem to affect Switch
views like it does text and other views. A workaround is to wrap each of the switches in a RelativeLayout
like the following:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
Upvotes: 2
Reputation: 2904
Here bro,
I adjust margin left and right.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="30sp"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center"
android:layout_weight="1"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="New"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_weight="1"
/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="New"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Hope this can help
Upvotes: 0