2016-09-28 3 views
2

Панель инструментов перекрывает GridView линейного макета, поэтому более 80% содержимого в первой строке скрывается на панели инструментов над ним.Панель инструментов перекрывает GridView LinearLayout

Это произошло после того, как я добавил FloatingActionButton в activity_main. Я попытался включить панель инструментов в LinearLayout, но это дало мне исключение. Если можно включить панель инструментов в LinearLayout, я в порядке с ней. Кроме того, я стараюсь не обрабатывать его статически, используя marginTop или paddingTop.

Вот как это выглядит сейчас: Screenshot

И вот XML-файлы:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
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:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="edu.ahduni.seas.gyapak.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

<include layout="@layout/content_main" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:id="@+id/textlayout" 
tools:context="edu.ahduni.seas.gyapak.MainActivity"> 

    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:columnWidth="90dp" 
     android:numColumns="auto_fit" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:stretchMode="columnWidth" 
     android:gravity="center" 
     android:padding="10dp" /> 
</LinearLayout> 

РЕШЕНИЯ

По-видимому, есть много решений для этого: (Заслуга ответы и комментарий обсуждения)

1. включают android:layout_marginTop="?attr/actionBarSize"android:paddingTop="AnydpThatYouFurtherWantToAdjust" в LinearLayout из content_main.xml кредита: @ VladimirJovanović

2. включают app:layout_behavior="@string/appbar_scrolling_view_behavior" android:paddingTop="AnydpThatYouFurtherWantToAdjust" в LinearLayout из content_main.xml Авторов: @Burhanuddin Рашид

3. Это длинная один, проверить @ ответ AbhayBohra и затем включить android:paddingTop="AnydpThatYouFurtherWantToAdjust" в LinearLayout из content_main.xml кредита: @Abhay Bohra и @Rahul Шарма

+0

попытку дать маржу верх в GridView в content_main – AbhayBohra

+0

@AbhayBohra Это уже решает эту проблему, но я не хочу его статическим. GridView должен начинаться с панели инструментов. –

+0

Попробуйте добавить paddingTop = "20dp" в content_main LinearLayout – Stallion

ответ

1

Добавить app:layout_behavior в свой content_main LinearLayout так:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
//This 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
android:id="@+id/textlayout" 
tools:context="edu.ahduni.seas.gyapak.MainActivity"> 

    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:gravity="center" 
     android:padding="10dp" /> 
</LinearLayout> 
+0

@ Это близко, но все же перекрывается, как 20%. –

+0

Нет, numColumns должен автоустанавливать доступную ширину и не должен дезориентироваться только в вертикальном порядке. Общий дизайн пользовательского интерфейса должен оставаться прежним. –

+0

Что вы подразумеваете под этим? Это близко, но все еще перекрывается, как 20% @ Mr.Robot –

2

Попробуйте положить appBarLayout и включают в себя заявление в LinearLayout с вертикальной ориентацией

<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="edu.ahduni.seas.gyapak.MainActivity"> 

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

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_main" /> 
    </LinearLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 
+0

А что это будет делать ??? объясните, пожалуйста? –

0

Просьба добавить tools:showIn="@layout/activity_main" к вашему content_main.xml LinearLayout. Конечный код должен выглядеть следующим образом:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/textlayout" 
    tools:showIn="@layout/activity_main" 
    tools:context="edu.ahduni.seas.gyapak.MainActivity"> 
+0

И что именно он будет делать? –

+1

PS: как @Abhay сказал добавить включить заявление также с вышеупомянутым ответом, и он должен работать как шарм. –

+1

@Robot [ВЕЩИ, ВЫ НЕ МОЖЕТЕ ЗНАТЬ: ИНСТРУМЕНТЫ АТРИБУТЫ] (http://www.randomlytyping.com/blog/2015/6/17/things-you-may-not-know-about-tools-attributes). Проверьте ссылку и перейти к указателям макета. Это может помочь вам понять. –

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