-1

我是新的android..我的英语一点点:(我想开发多窗格应用程序。两个片段“Gridview”“全屏” 我知道多窗格,但我不知道多窗格中的Gridview和全屏。因为所有示例对于 ListView。请帮帮我

这个应用程序对我很好,但太复杂了= http://www.codeproject.com/Articles/779293/Building-Dynamic-UI-for-Android-Devices

4

1 回答 1

0

如果我正确理解您的问题,这是一个简单的实现

------------- 创建 GridView ------------

  1. 对于 GridView,您需要 GridView 对象的适配器和列的图像,使用国家列表和标志图像的示例,

在 res 中创建 drawable 文件夹并在 drawable 文件夹中添加国旗图像

  1. 在 res/layout 中创建 gridlayout.xml

       <RelativeLayout  
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@drawable/border"
        android:padding="5dp">
    
        <ImageView 
        android:id="@+id/gridimage" 
        android:layout_height="65dp" 
            android:layout_width="65dp" 
            android:src="@drawable/icon" 
            android:layout_alignParentTop="true" 
            android:layout_centerHorizontal="true">
        </ImageView>
    
        <TextView 
            android:id="@+id/gridtext"
            android:text="TextView" 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_below="@+id/imageView1" 
            android:layout_marginTop="2dp"
            android:layout_centerHorizontal="true"
            android:textSize="18sp"
            android:ellipsize="marquee"></TextView>
    
    </RelativeLayout>
    
  2. 在 res/Layout 文件夹中创建 gridrow.xml

    <GridView 
        android:id="@+id/gridView1" 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp">
    </GridView>
    

  3. 创建 GridAdapter.java 类

    public class GridAdapter extends BaseAdapter { 
        private ArrayList<String> CountryList;
                private ArrayList<Integer> CountryFlag;
                private Activity activity;
                public GridAdapter(Activity activity,ArrayList<String> CountryList, ArrayList<Integer> CountryFlag){
    super();this.CountryList = CountryList;
                this.CountryFlag = CountryFlag;
                this.activity = activity;}
        @Override
            public int getCount() {
                return CountryList.size();
            }
    
            @Override
            public String getItem(int position) {
                return CountryList.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return 0;
            }
    
            public static class ViewHolder
            {
                public ImageView imgViewFlag;
                public TextView txtViewTitle;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder view;
                LayoutInflater inflator = activity.getLayoutInflater();
    
                if(convertView==null)
                {
                    view = new ViewHolder();
                    convertView = inflator.inflate(R.layout.gridview_row, null);
    
                    view.txtViewTitle = (TextView) convertView.findViewById(R.id.gridtext);
                    view.imgViewFlag = (ImageView) convertView.findViewById(R.id.gridimage);
    
                    convertView.setTag(view);
                }
                else
                {
                    view = (ViewHolder) convertView.getTag();
                }
    
                view.txtViewTitle.setText(CountryList.get(position));
                view.imgViewFlag.setImageResource(CountryFlag.get(position));
    
                return convertView;
            } 
    

    }

    1. 创建片段 GridViewActivty

    public class GridViewActivty extends Fragment { private ArrayList CountryList = new ArrayList(); private ArrayList CountryFlag = new ArrayList(); private GridView mygrid; private GridAdapter adapter; private Context context; public GridViewActivty(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.gridlayout.xml, container, false);

              CountryList = new ArrayList<String>();
              CountryList.add("South Africa");
              CountryList.add("Spain");
              CountryList.add("Canada");
              CountryList.add("China");
              CountryList.add("France");
              CountryList.add("Germany");
              CountryList.add("Iran");
              CountryList.add("Italy");
              CountryList.add("Japan");
              CountryList.add("Korea");
              CountryList.add("Mexico");  
              CountryList.add("Netherlands");
              CountryFlag = new ArrayList<Integer>();
                  CountryFlag.add(R.drawable.southafrica);
                  CountryFlag.add(R.drawable.spain);
                  CountryFlag.add(R.drawable.canada);
                  CountryFlag.add(R.drawable.china);
                  CountryFlag.add(R.drawable.france);
                  CountryFlag.add(R.drawable.germany);
                  CountryFlag.add(R.drawable.iran);
                  CountryFlag.add(R.drawable.italy);
                  CountryFlag.add(R.drawable.japan);
                  CountryFlag.add(R.drawable.korea);
                  CountryFlag.add(R.drawable.mexico);
                  CountryFlag.add(R.drawable.netherlands);
                 adapter = new GridAdapter(this.getActivity(),CountryList, CountryFlag);
    
            mygrid = (GridView)rootView.findViewById(R.id.gridview1);
    
            mygrid.setAdapter(adapter);
            mygrid.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                        long arg3) {
                    // TODO Auto-generated method stub
    
                }
            }
                    );
            return rootView;
    
            }
    
    }
    

    ----------- Creating Full Screen --------
    
  4. 在布局文件夹中创建 fullscreen.xml

        <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </RelativeLayout>    
    

    >

  5. 创建 FullScreen.java 片段

     public class Fullscreen extends Fragment{    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle 
             savedInstanceState)    
    { View rootView = inflater.inflate(R.layout.fullscreen.xml, container, false);
             requestWindowFeature(Window.FEATURE_NO_TITLE);
    
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    
             WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
                }
    
            return convertView; }
    
于 2015-01-31T21:38:37.813 回答