2

Я использую Linear Layout для этого XML-файла:Android: как отцентрировать отдельные взгляды, когда сила тяжести компоновщика фиксировано

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:gravity="center" 
    > 



    <EditText 
     android:id="@+id/editTextUserNameToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="User Name"> 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editTextPasswordToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Password" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/buttonSignIn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Sign In" /> 

    <ToggleButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New ToggleButton" 
     android:id="@+id/toggleButton"/> 

</LinearLayout> 

Я хочу все виды текста появится в центре экрана, за исключением переключатель, который я хочу, чтобы он находился в правом нижнем углу экрана.

Как вы можете видеть, я использую центр android:gravity в теге макета, потому что ни это, ни android:layout_gravity не работали над отдельными текстовыми изображениями.

Но теперь я хочу, чтобы кнопка переключения не зависела от этого. Как я могу это достичь?

ответ

0

Вы можете использовать относительный макет выглядеть этот код, как вы можете установить свой XML в соответствии с вашими требованиями.

<?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:gravity="center" 
android:orientation="vertical"> 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editTextUserNameToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_centerInParent="true" 
     android:hint="User Name"> 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editTextPasswordToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Password" 
     android:layout_below="@+id/editTextUserNameToLogin" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/buttonSignIn" 
     android:layout_width="fill_parent" 
     android:layout_below="@+id/editTextPasswordToLogin" 
     android:layout_height="wrap_content" 
     android:text="Sign In" /> 

    <ToggleButton 
     android:id="@+id/toggleButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="New ToggleButton" /> 
</RelativeLayout> 

Это то, что вы хотите. Благодаря

1

тест этот XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/editTextUserNameToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="User Name" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editTextPasswordToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Password" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/buttonSignIn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Sign In" /> 
</LinearLayout> 

<ToggleButton 
    android:id="@+id/toggleButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New ToggleButton" /> 

1

Просто обернуть весь макет в FrameLayout и удерживать кнопку переключения вне LinearLayout. затем поставьте layout_gravity как пожелаете. Это трюк. ура :) Попробуйте

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_gravity="center" 
    > 
    <EditText 
     android:id="@+id/editTextUserNameToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="User Name"> 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editTextPasswordToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Password" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/buttonSignIn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Sign In" /> 


    </LinearLayout> 
    <ToggleButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New ToggleButton" 
    android:layout_gravity="bottom|right" 
     android:id="@+id/toggleButton"/> 
    </FrameLayout> 
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:gravity="center" 
android:orientation="vertical"> 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editTextUserNameToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_centerInParent="true" 
     android:hint="User Name"> 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editTextPasswordToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Password" 
     android:layout_below="@+id/editTextUserNameToLogin" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/buttonSignIn" 
     android:layout_width="fill_parent" 
     android:layout_below="@+id/editTextPasswordToLogin" 
     android:layout_height="wrap_content" 
     android:text="Sign In" /> 

    <ToggleButton 
     android:id="@+id/toggleButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="New ToggleButton" /> 
</RelativeLayout> 
Смежные вопросы