2016-04-07 12 views
0

私はこのレイアウトを持っています。AndroidのGridViewレイアウトに問題がある

<?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="com.example.anandvardhan.albumview040416.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> 

    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/grid_view" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:numColumns="2" 
       android:gravity="center" 
       android:background="#fff"> 
    </GridView> 

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

しかし、今私はこのようなプレビューを取得しています:それは、ツールバーの下でなければならないため、1位の項目を選択するにはどのように

But now I am getting a preview like this:

さらにもう1つ。

GridViewが最初に空の場合は、「空です」と表示されている 画面の中央にTextViewが表示されている必要があります。それを動的に設定する方法。

誰かが助けてくれれば非常に感謝しています。

+0

コードを投稿してください。 – Aks4125

+0

'XML'の位置1で' GridView'アイテムを達成しようとしていますか? – Aks4125

+0

@ Aks4125:デフォルトでは、項目1から開始する必要があります。しかしプレビューではアイテム3から表示されています。 –

答えて

0

何か質問があれば教えてください、

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    tools:context="com.example.anandvardhan.albumview040416.MainActivity"> 

    <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.CoordinatorLayout 

     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     > 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

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

     <GridView android:id="@+id/grid_view" 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="#fff" 
        android:gravity="center" 
        android:numColumns="2"> 
     </GridView> 

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

</LinearLayout> 
+0

本当にありがとうございました。あなたは私の一日を救った。 –

+0

@AnandVardhan問題ありません。 =) – Aspicas

0

使用RecyclerViewとGridLayoutManagerとして設定レイアウトマネージャ。ここで

がRecyclerViewでGridLayoutManagerを使用するXML

<?xml version="1.0" encoding="utf-8"?> 

<?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="com.example.anandvardhan.albumview040416.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> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

<!-- Your Empty view here --> 

    <android.support.design.widget.FloatingActionButton 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_marginBottom="20dp" 
     android:layout_marginRight="20dp" 
     app:backgroundTint="@color/button_background_dark_red" 
     android:src="@drawable/ic_action_new" 
     app:borderWidth="0dp" 
     app:elevation="6dp" 
     app:fabSize="normal"/> 

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

だ、何もデータがありません場合は、あなたがView.GONEにRecyclerViewの可視性を設定し、空のビューの表示を設定することができますthis site

をご確認ください。 View.VISIBLEへ

+0

ありがとう、私はこれを試して、できるだけ早く通知します。 –

関連する問題