0

我想像使用 textviews 一样将两个开关居中,但我尝试了所有可能的选项但没有成功。

这是代码的简短版本:

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

截屏

4

4 回答 4

2

问题是该gravity属性似乎不会Switch像文本和其他视图那样影响视图。一种解决方法是将每个开关包装RelativeLayout成如下所示:

    <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>
于 2014-12-26T12:32:15.877 回答
1

试试这个我测试过

 <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>
于 2014-12-26T12:30:13.347 回答
0

来了兄弟

我左右调整边距。

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

希望这可以帮助

于 2014-12-26T12:29:13.013 回答
0

将 center_vertical 重力设置为 TextView 并切换父 LinearLayout :

android:gravity="center_vertical"
于 2014-12-26T12:33:42.167 回答