【问题标题】:Vertically center two switches垂直居中两个开关
【发布时间】:2014-12-26 12:17:47
【问题描述】:

我想像使用 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>

【问题讨论】:

    标签: android android-layout android-switch


    【解决方案1】:

    问题在于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>
    

    【讨论】:

      【解决方案2】:

      试试这个我测试过的

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

      【讨论】:

      • 谢谢!有用。但是现在我尝试使用 Horizo​​ntal LinearLayouts 而不是 FrameLayouts 并且也可以,最好的解决方案是什么?
      • 很高兴听到这个消息。请参考this
      【解决方案3】:

      兄弟,

      我左右调整边距。

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

      希望对你有帮助

      【讨论】:

        【解决方案4】:

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

        android:gravity="center_vertical"
        

        【讨论】:

          猜你喜欢
          • 2019-12-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-15
          相关资源
          最近更新 更多