2015-02-25 4 views
0

Ищите советы о том, как лучше выровнять эти кнопки. Наверное, есть много способов сделать это. Возможно, центрируйте все три кнопки по вертикали, так как они разные кнопки размера? Прямо сейчас кнопки просто выглядят беспорядочно, потому что я совершенно новый для Android-программирования.Выравнивание кнопок в Android

Вот текущий код кнопки:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.redacted.redacted.HomeScreen$PlaceholderFragment" 
    android:background="#ff6d8416"> 

<TextView android:id="@+id/section_label" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Beginner/Principiante" 
    android:id="@+id/beginnerButton" 
    android:layout_marginBottom="26dp" 
    android:layout_above="@+id/intermediateButton" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Intermediate/Intermedio" 
    android:id="@+id/intermediateButton" 
    android:layout_alignRight="@+id/beginnerButton" 
    android:layout_alignEnd="@+id/beginnerButton" 
    android:layout_centerVertical="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Advanced/Advanzado" 
    android:id="@+id/advancedButton" 
    android:layout_marginTop="28dp" 
    android:layout_below="@+id/intermediateButton" 
    android:layout_alignLeft="@+id/intermediateButton" 
    android:layout_alignStart="@+id/intermediateButton" 
    android:layout_centerVertical="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="redacted" 
    android:id="@+id/textView" 
    android:layout_alignTop="@+id/section_label" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:textColor="@android:color/white" /> 

</RelativeLayout> 

Это мой текущий результат

enter image description here

+0

Вы можете разместить ссылку на изображение, где-то, как imgur.com то пользователи с достаточно респ можно добавить изображения для вас. Изображения того, что вы получаете и чего хотите, очень полезны для этих типов вопросов. Прямо сейчас, немного непонятно, что вы хотели бы совершить – codeMagic

+0

Нет проблем, вот как выглядят кнопки до сих пор: http://imgur.com/kUpCp8M – joshgoldeneagle

+0

, пожалуйста, отправьте снимок экрана –

ответ

1

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

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_gravity="center" 
    android:padding="20dp" 
    android:layout_height="wrap_content"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Beginner" 
     android:layout_margin="10dp" 
     android:id="@+id/button" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Intermediate" 
     android:layout_margin="10dp" 
     android:id="@+id/button2" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Advanced" 
     android:layout_margin="10dp" 
     android:id="@+id/button3" /> 
</LinearLayout> 
0

Используйте вертикальную LinearLayout и установить поля для создания пробелов вокруг них.

+0

Я сделал линейный макет и использовал предложения людей по удалению кода, связанного с относительной компоновкой. Я бы опубликовал свой точный xml-код, но Stack Overflow ограничивает мой размер комментария. Всем спасибо! – joshgoldeneagle

1

Вертикально ориентированный LinearLayout с несколькими изменениями должен отлично работать для вас. Вот пример

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical"> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Beginner/Principiante" 
     android:id="@+id/beginnerButton" 
     android:layout_marginBottom="26dp" 
     android:layout_gravity="center_horizontal"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Intermediate/Intermedio" 
     android:id="@+id/intermediateButton" 
     android:layout_centerVertical="true" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Advanced/Advanzado" 
     android:id="@+id/advancedButton" 
     android:layout_marginTop="28dp" 
     android:layout_gravity="center_horizontal"/> 
</LinearLayout> 

Запомните изменения, которые я сделал. Особенно понравилось gravity и удалено то, что возможно RelativeLayout.LayoutParams, что не нужно. Если это не сработает (у меня еще не было возможности протестировать), обертывание Button s в другом LinearLayout и при условии, что будет работать gravity="center".

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