2016-01-24 4 views
3

Я попытался добавить 4 кнопки в макет сетки. В равном промежутке я использовал colum и row Weight, но все же я не могу выровнять все одинаковые интервалы, я получаю результат следующим образом. Упомянуто ниже на изображении. enter image description hereНевозможно исправить 4 кнопки в макете сетки

<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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.rahulcomp24.gridlayoutdemo.MainActivity"> 

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_row="0" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     android:text="New Button" 
     android:id="@+id/button" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:layout_row="0" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     android:text="New Button" 
     android:id="@+id/button2" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_row="1" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     android:text="New Button" 
     android:id="@+id/button3" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:layout_row="1" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     android:text="New Button" 
     android:id="@+id/button4" /> 
</GridLayout> 

Я использую MinSdk версии 19.

enter image description here

+0

Вашего макета работает отлично на моей установка протестирована на мин 19 –

+0

SDK @rupesh_padhye с таким же кодом? –

+0

Да с тем же кодом –

ответ

1

Скопируйте приведенный ниже xml как есть и запустите его, вы увидите ту же кнопку пробела.

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

<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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill" 
      android:layout_row="0" 
      android:layout_rowWeight="1" 
      android:text="New Button" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill" 
      android:layout_row="0" 
      android:layout_rowWeight="1" 
      android:text="New Button" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill" 
      android:layout_row="1" 
      android:layout_rowWeight="1" 
      android:text="New Button" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill" 
      android:layout_row="1" 
      android:layout_rowWeight="1" 
      android:text="New Button" /> 
    </GridLayout> 

</RelativeLayout> 
+0

Не работает –

+0

Его работа отлично здесь, вам не нужно ничего менять, просто измените layout_gravity только кнопки 3 и 4 – Pankaj

+0

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

0

Вы можете использовать два LinearLayouts вместо, по одному для каждой строки, а также заменить родительский RelativeLayout с другим LinearLayout.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="2"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:weightSum="2" 
    android:orientation="horizontal"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 


    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:weightSum="2" 
    android:orientation="horizontal"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 


    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 

</LinearLayout> 

</LinearLayout> 
+0

Я хочу сделать это, используя Grid Layout. Какая ошибка в моем методе компоновки сетки? –

+0

Я скопировал ваш код в моей собственной среде IDE, и просмотр был в порядке, не видел никаких проблем –

0

Ваш фрагмент кода работает fine.You может быть сталкиваются с проблемами из-за какой-либо другой макет paramete rs в вашем проекте.

Я рекомендую попробовать создать новый проект и проверить макет снова Snapshot

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