Android 选项卡内的网格布局

Android 选项卡内的网格布局,android,android-fragments,android-viewpager,android-gridview,android-tabs,Android,Android Fragments,Android Viewpager,Android Gridview,Android Tabs,我是Android新手,因此面临着这样的问题 如何从以下位置更改布局: 致: XML片段\u main: <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_paren

我是Android新手,因此面临着这样的问题

如何从以下位置更改布局:

致:

XML片段\u main:

<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.example.snbgearassistant.MainActivity$PlaceholderFragment" >


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

</RelativeLayout>


因此,我需要这些选项卡具有不同内容的网格布局。

您必须在ViewPager中使用网格视图。因此,在您的
main活动中
,您将拥有此布局

创建activity_main.xml布局 这是主要布局。所有的东西都在里面,包括你的片段和标签

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.gridview.MainActivity" />
现在,让我们定义一个片段类,它负责扩展这个布局并处理视图

创建一个片段以膨胀GridView布局:PlaceHolderFragment.java 现在,我们必须创建一个适配器类来处理
GridView
的每个项,这样您就可以管理每个项的行为

创建适配器以支持GridView项:MyAdapter.java 正如您在这里看到的,我们通过将一些项添加到适配器类末尾定义的
Item
类型的
ArrayList
来将它们添加到GridView

private class MyAdapter extends BaseAdapter
{
        private List<Item> items = new ArrayList<Item>();
        private LayoutInflater inflater;

        public MyAdapter(Context context)
        {
            inflater = LayoutInflater.from(context);

            items.add(new Item("Image 1", Color.GREEN));
            items.add(new Item("Image 2", Color.RED));
            items.add(new Item("Image 3", Color.BLUE));
            items.add(new Item("Image 4", Color.GRAY));
            items.add(new Item("Image 5", Color.YELLOW));
        }

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

        @Override
        public Object getItem(int i)
        {
            return items.get(i);
        }

        @Override
        public long getItemId(int i)
        {
            return items.get(i).colorId;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View v = view;
            ImageView picture;
            TextView name;

            if(v == null)
            {
                v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
                v.setTag(R.id.picture, v.findViewById(R.id.picture));
                v.setTag(R.id.text, v.findViewById(R.id.text));
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)getItem(i);

            picture.setBackgroundColor(item.colorId);
            name.setText(item.name);

            return v;
        }

        private class Item
        {
            final String name;
            final int colorId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.colorId = drawableId;
            }
        }
    }
现在我们必须为
GridView
项定义XML布局

创建XML布局gridview_item.XML 如您所见,这里我们向布局中添加了两项。一个是
SquareImageView
(我们在上面创建的类)类型的元素,另一个是
TextView
,它是每个图像的标签

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.myapp.gridview.SquareImageView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:layout_gravity="bottom"
        android:textColor="@android:color/white"
        android:background="#55000000"
        />
</FrameLayout>

在这里,我测试了代码,这是最终结果。当然,您会为图像更改这些颜色,但这是您应该遵循的方法

注意:要在
MyAdapter
类的
getView()
方法中为
GridView
项设置图像而不是颜色,请使用而不是
setBackgroundColor(int)


您必须在ViewPager中使用GridView。因此,在您的
main活动中
,您将拥有此布局

创建activity_main.xml布局 这是主要布局。所有的东西都在里面,包括你的片段和标签

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.gridview.MainActivity" />
现在,让我们定义一个片段类,它负责扩展这个布局并处理视图

创建一个片段以膨胀GridView布局:PlaceHolderFragment.java 现在,我们必须创建一个适配器类来处理
GridView
的每个项,这样您就可以管理每个项的行为

创建适配器以支持GridView项:MyAdapter.java 正如您在这里看到的,我们通过将一些项添加到适配器类末尾定义的
Item
类型的
ArrayList
来将它们添加到GridView

private class MyAdapter extends BaseAdapter
{
        private List<Item> items = new ArrayList<Item>();
        private LayoutInflater inflater;

        public MyAdapter(Context context)
        {
            inflater = LayoutInflater.from(context);

            items.add(new Item("Image 1", Color.GREEN));
            items.add(new Item("Image 2", Color.RED));
            items.add(new Item("Image 3", Color.BLUE));
            items.add(new Item("Image 4", Color.GRAY));
            items.add(new Item("Image 5", Color.YELLOW));
        }

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

        @Override
        public Object getItem(int i)
        {
            return items.get(i);
        }

        @Override
        public long getItemId(int i)
        {
            return items.get(i).colorId;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View v = view;
            ImageView picture;
            TextView name;

            if(v == null)
            {
                v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
                v.setTag(R.id.picture, v.findViewById(R.id.picture));
                v.setTag(R.id.text, v.findViewById(R.id.text));
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)getItem(i);

            picture.setBackgroundColor(item.colorId);
            name.setText(item.name);

            return v;
        }

        private class Item
        {
            final String name;
            final int colorId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.colorId = drawableId;
            }
        }
    }
现在我们必须为
GridView
项定义XML布局

创建XML布局gridview_item.XML 如您所见,这里我们向布局中添加了两项。一个是
SquareImageView
(我们在上面创建的类)类型的元素,另一个是
TextView
,它是每个图像的标签

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.myapp.gridview.SquareImageView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:layout_gravity="bottom"
        android:textColor="@android:color/white"
        android:background="#55000000"
        />
</FrameLayout>

在这里,我测试了代码,这是最终结果。当然,您会为图像更改这些颜色,但这是您应该遵循的方法

注意:要在
MyAdapter
类的
getView()
方法中为
GridView
项设置图像而不是颜色,请使用而不是
setBackgroundColor(int)

罗格给出的答案非常好。但每个片段的图像是相同的。我喜欢在带有viewpager的mainactivity中添加一些代码。我想我们可以使用fragment代替activity,下面是代码。与rogcg给出的mainactivity代码相同。也添加这些代码。
在mainfragment的布局中添加ActionBarlayout、工具栏和幻灯片布局
在Mainfragment中,添加
私有列表mFragments=新向量();
在oncreate()中,创建三个片段,
添加(新的HomeFragment());
添加(新标题1());
添加(新标题2());
添加(新标题3());
在onCreateView()中,添加
mSectionsPagerAdapter=新的SectionsPagerAdapter(getChildFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout=(slidengtablayout)v.findviewbyd(R.id.tabanim_选项卡);
tabLayout.setViewPager(mviewpage);
在SectionPageAdapter类中,添加
@凌驾
公共片段getItem(int位置){
返回mFragments.get(位置+1);
}
@凌驾
public int getCount(){
返回3;
}
@凌驾
公共字符序列getPageTitle(int位置){
Locale l=Locale.getDefault();
开关(位置)
{
案例0:
返回getString(R.string.English).toUpperCase(l);
案例1:
返回getString(R.string.Tamil).toUpperCase(l);
案例2:
返回getString(R.string.Hindi).toUpperCase(l);
}
返回null;
}
现在在Title1()片段中添加任何视图,并在其中添加任何内容,我认为这条消息很有用。请投我一票。非常感谢。
罗格给出的答案非常好。但每个片段的图像是相同的。我喜欢在带有viewpager的mainactivity中添加一些代码。我想我们可以使用fragment代替activity,下面是代码。与rogcg给出的mainactivity代码相同。也添加这些代码。
在mainfragment的布局中添加ActionBarlayout、工具栏和幻灯片布局
在Mainfragment中,添加
私有列表mFragments=新向量();
在oncreate()中,创建三个片段,
添加(新的HomeFragment());
添加(新标题1());
添加(新标题2());
添加(新标题3());
在onCreateView()中,添加
mSectionsPagerAdapter=新的SectionsPagerAdapter(getChildFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout=(slidengtablayout)v.findviewbyd(R.id.tabanim_选项卡);
tabLayout.setViewPager(mviewpage);
在里面
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.myapp.gridview.SquareImageView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:layout_gravity="bottom"
        android:textColor="@android:color/white"
        android:background="#55000000"
        />
</FrameLayout>
Answer Given By rogcg is very good and nice. But the Images for each fragment is same. I like to add some codes in the mainactivity which has viewpager.I think we can use fragment instead of activity, Here is the code.The same code as the Main Activity given by rogcg. Add these codes too.
In Layout for mainfragment add ActionBarlayout,toolbar and slidingtablayout
In Mainfragment,add 
  private List<Fragment> mFragments = new Vector<Fragment>();
 in oncreate(), create three fragments , 
    mFragments.add(new HomeFragment());
    mFragments.add(new Title1());
    mFragments.add(new Title2());
    mFragments.add(new Title3());
  in onCreateView(),add 
      mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
    mViewPager.setAdapter(mSectionsPagerAdapter);
    tabLayout = (SlidingTabLayout) v.findViewById(R.id.tabanim_tabs);
    tabLayout.setViewPager(mViewPager);
  in SectionPageAdapter class,add
    @Override
    public Fragment getItem(int position) {

        return mFragments.get(position+1);
    }

    @Override
    public int getCount() {

        return 3;

    }

    @Override
    public CharSequence getPageTitle(int position) {
      Locale l = Locale.getDefault();
        switch (position)
        {
            case 0:
                return getString(R.string.English).toUpperCase(l);
            case 1:
                return getString(R.string.Tamil).toUpperCase(l);
            case 2:
                return getString(R.string.Hindi).toUpperCase(l);
        }
        return null;

    }
 Now add any view in Title1() fragment as you usage and add any things in it I think this message was useful. please vote for me. Thank you.