2013-05-09 3 views
0
<Button 
    android:id="@+id/button1" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:text="Click Me" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/button1" 
    android:text="Click me1" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/button1" 
    android:text="Click Me2" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/button3" 
    android:layout_below="@+id/button2" 
    android:text="Click me3" /> 

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

Я хочу, чтобы добиться вывода таким образом, что он должен выглядеть:

 Button1 Button2 
     Button3 Button4 

И весь комплект должен быть коллективно в центре экрана вообще является размер экрана.

+0

[проверить здесь] (HTTP: //stackoverflow.com/a/1501534/2345913) – CRUSADER

ответ

3

Использование LinearLayout с layout_width = "maych_parent", layout_height = "match_parent", гравитация = "центр", ориентация = "вертикальный"

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:text="Click Me" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:text="Click me1" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:text="Click Me2" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:text="Click me3" /> 
</LinearLayout> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:text="Click Me2" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:text="Click me3" /> 
</LinearLayout> 

</LinearLayout> 
+0

Спасибо за ваше время. В приведенном примере выровняйте кнопку в вертикальном порядке в центре экрана. Ничего страшного. Но как мне сделать, как button1 button2, тогда под этими двумя будет кнопка button3 и все вместе в центре. – Amrit

+0

Я меняю свой код, как то, что вы хотите –

+0

Получил это! Большое спасибо!! :) – Amrit

0

Попробуйте это:

layout_width="fill_parent" 
layout_marginLeft="10dip" 
layout_marginRight="10dip" 

Вы также можете использовать Relative layout и установить ребенка android:layout_centerInParent="true"

0

Adjust layout_y и _x, то это будет место в центре

<Button 
android:id="@+id/Button01" 
android:layout_width="188px" 
android:layout_height="100px" 
android:text="A" 
android:layout_y="50px" android:layout_x="65px" android:textSize="48sp"/> 
0

Использование LinearLayout с тяжесть центр

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

<Button 
    android:id="@+id/button1" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:text="Click Me" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/button1" 
    android:text="Click me1" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/button1" 
    android:text="Click Me2" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/button3" 
    android:layout_below="@+id/button2" 
    android:text="Click me3" /> 


</LinearLayout> 

Output

+0

Спасибо за ваше время. В приведенном примере выровняйте кнопку в вертикальном порядке в центре экрана. Ничего страшного. Но как мне сделать, как button1 button2, тогда под этими двумя будет кнопка button3 и все вместе в центре. – Amrit

+0

@LetsCode Вы можете использовать линейную компоновку для каждой из двух кнопок, с горизонтальной ориентацией для каждого линейного макета. –

+0

Спасибо, друг! Это помогло много :) – Amrit

Смежные вопросы