2017-02-23 80 views
0

我剛剛創建了一個簡單的網格視圖,我想在網格視圖頁面中添加一個選項卡布局和一個工具欄作爲工具欄和選項卡布局顯示在網格的正面查看它看起來像這樣enter image description here這是我的XML代碼如何在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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context=".MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     <!-- To scroll these tab, use app:tabMode="scrollable" --> 
     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabGravity="fill" 
      app:tabMode="fixed" /> 
    </android.support.design.widget.AppBarLayout> 

    <GridView 
     android:id="@+id/gridView1" 
     android:layout_width="fill_parent" 
     android:background="#d2d2d2" 
     android:layout_height="fill_parent" 
     android:layout_margin="4dp" 
     android:columnWidth="80dp" 
     android:gravity="center" 
     android:horizontalSpacing="5dp" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" > 
    </GridView> 

</RelativeLayout> 
+0

你能張貼圖片嗎? –

+0

當然我會在那裏添加圖片 – 7arooney

+0

@Drew這裏是我發佈的圖片,如果你看到那裏有一些網格物品不會出現作爲工具欄和標籤佈局覆蓋它 – 7arooney

回答

0

請嘗試使用以下步驟。希望你的問題能夠解決。若要將網格視圖右側下coordinatorLayout我已經修改XML文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="themovie.com.javarank.materialdesignpractice.MainActivity"> 
    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/coordinatorLayout" 
     > 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabGravity="fill" 
      app:tabMode="fixed" /> 
    </android.support.design.widget.CoordinatorLayout> 
    <GridView 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:background="#d2d2d2" 
     android:layout_height="wrap_content" 
     android:layout_margin="4dp" 
     android:columnWidth="80dp" 
     android:gravity="center" 
     android:horizontalSpacing="5dp" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" 
     android:layout_below="@+id/coordinatorLayout" 
     > 
    </GridView> 
</RelativeLayout> 

現在通過使用您的圖像將顯示其創建網格視圖自定義佈局。我把它命名爲grid_view_custom_layout

<?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_height="match_parent"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     /> 
</LinearLayout> 

我們表明,我們需要通過擴展BaseAdapter類來創建自定義適配器類將數據添加你需要提供執行的一些方法。 我有一個名爲類作爲GridViewAdapter

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 

import java.util.ArrayList; 
import java.util.List; 



public class GridViewAdapter extends BaseAdapter { 

    private Context context; 
    private List<Integer> images = new ArrayList<Integer>(); 

    public GridViewAdapter(Context context, List<Integer> images){ 
     this.context = context; 
     this.images = images; 
    } 


    @Override 
    public int getCount() { 
     return images.size(); 
    } 

    @Override 
    public Object getItem(int i) { 
     return null; 
    } 

    @Override 
    public long getItemId(int i) { 
     return 0; 
    } 

    private static final class ViewHolder { 
     private ImageView imageView; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if(convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.grid_view_custom_layout, null, true); 
      holder = new ViewHolder(); 
      holder.imageView = (ImageView) convertView.findViewById(R.id.imageView); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.imageView.setImageResource(images.get(position)); 

     return convertView; 
    } 
} 

我們初始化GridViewAdapter我們需要上下文和整數的集合。

要獲取上下文,只需調用getApplicationContext()並設置Integer的集合。爲了構建Integer的集合,我使用了可繪製文件夾中的一些圖像。你將需要使用你的圖片。代碼在這裏。

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.widget.GridView; 

import java.util.ArrayList; 


public class MainActivity extends AppCompatActivity { 

    private Toolbar myToolBar; 

    private GridView gridView1; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ArrayList<Integer> images = new ArrayList<Integer>(); 
     images.add(R.drawable.image); 
     images.add(R.drawable.image2); 
     images.add(R.drawable.image3); 
     images.add(R.drawable.image); 
     images.add(R.drawable.image2); 
     images.add(R.drawable.image3); 
     images.add(R.drawable.image); 
     images.add(R.drawable.image2); 
     images.add(R.drawable.image3); 

     GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), images); 
     gridView1 = (GridView) findViewById(R.id.gridView1); 
     gridView1.setAdapter(adapter); 

    } 
} 

如果解決了您的問題,請將其標記爲解決方案。

+0

非常感謝兄弟我要試試這段代碼並回復給你:) – 7arooney

+0

實際上它運行良好,但它只顯示工具欄,它沒有顯示標籤佈局 – 7arooney

+0

我沒有添加標籤佈局,我只是在網格視圖中顯示數據。如果您的要求顯示標籤,它可以很容易地完成。如果你需要標籤的示例代碼,請告訴我。我會爲你做的。如果它解決了您的問題,請將其標記爲答案。這對其他人解決他們的問題會有好處。 –

0

如果你想使用AppBarLayout正常,你必須使用一個android.support.design.widget.CoordinatorLayout作爲父母的佈局。

然後您需要將app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到您的GridView。另外不要忘記使用NoActionBar -Theme爲您的Activity

對於標籤我會使用ViewPagerFragmentStatePagerAdapter。只要將GridView到一個單獨的片段佈局文件(顯然app:layout_behavior應該被移到ViewPager)和加載不同的Fragment有給你的標籤,這樣的:

public class MyAdapter extends FragmentStatePagerAdapter { 
    public MyAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public int getCount() { 
     return 3; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch(position) { 
      case 0: case 1: 
       return new GridFragment(); 
      default: 
       return new OtherFragment(); 
     } 
    } 
} 

現在你可以重用GridView,並使用不同的佈局爲其他選項卡。 欲瞭解更多信息,請參閱Android Developer References

+0

非常感謝它現在真的有用很感謝@Devenias: D,但我應該不得不添加一個ViewPager? – 7arooney

+0

@ 7arooney我認爲使用ViewPager是管理標籤的最簡單方法。您也可以通過自己更換片段來獲得類似的結果,但我沒有看到任何優勢。 – Devenias

+0

aha我得到它感謝您的回覆它真的很有幫助....因爲我要添加這些標籤的片段,但我需要這個我的XML文件的主要和第一個標籤,所以我應該把網格視圖單獨片段或在這個活動中保持這個網格視圖?@Devenias – 7arooney

相關問題