2017-09-27 37 views
1

我在我的活動中添加了一個開關並將其拖入中心。當我啓動我的應用程序時,開關位於左上角。我似乎無法得到它的工作。請看代碼和圖片。謝謝。我也有交換機上的驗證碼我如何獲得中心的開關

<Switch 
     android:id="@+id/switch1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:foregroundGravity="center_horizontal" 
     android:gravity="center" 
     android:text="" 
     tools:ignore="MissingConstraints" 
     tools:layout_editor_absoluteX="172dp" 
     tools:layout_editor_absoluteY="242dp" /> 

enter image description here

+0

圖片在哪裏? –

+0

什麼是父級佈局? –

+0

constrait佈局 – Airspir

回答

0

試試這個代碼

<RelativeLayout 

    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <Switch 
     android:id="@+id/switch1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:checked="true" 
     android:foregroundGravity="center_horizontal" 
     android:gravity="center" 
     android:text=""/> 
</RelativeLayout> 
0

添加父佈局的重心: 安卓重力= 「中心」。

+0

沒有做任何事 – Airspir

+0

這適用於父線性佈局。如果你是在相對佈局使用它,請加在你切換: 機器人:layout_centerInParent =「真」 的屏幕或 機器人的中心:layout_centerHorizo​​ntal =「真」 用於水平中心和 機器人:layout_centerVertical =」真「 垂直居中。確保你的父母的寬度和高度匹配父母。 – Satya

0

如果父母佈局是線性佈局:

<Switch 
    android:id="@+id/switch1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checked="true" 
    android:foregroundGravity="center_horizontal" 
    android:gravity="center" 
    android:text="" 
    android:layout_gravity="center" 
    tools:ignore="MissingConstraints" 
    tools:layout_editor_absoluteX="172dp" 
    tools:layout_editor_absoluteY="242dp" /> 

// just added android:layout_gravity="center" 

在相對佈局

<Switch 
    android:id="@+id/switch1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checked="true" 
    android:text="" 
    android:layout_centerInParent="true" 
    tools:ignore="MissingConstraints" 
    tools:layout_editor_absoluteX="172dp" 
    tools:layout_editor_absoluteY="242dp" /> 
0

您使用ConstraintLayout,通過拖動佈局工具的情況下:絕對性能更新,這些被用來僅適用於AndroidStudio編輯器

真實位置基於app:constraint標籤。

如果你不熟悉LayoutManagers,我認爲你應該從一個更簡單的FrameLayout,LinearLayout,RelativeLayout開始,作爲你的視圖的根。

所有提到的都支持childrem上的android:layout_gravity =「center」或所有childrem的父級上的android:gravity =「true」。

相關問題