程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> layoutinflater-android studio gridview如何實現

layoutinflater-android studio gridview如何實現

編輯:編程綜合問答
android studio gridview如何實現

我實現的老是黑屏退出
mainactivity.java

 private LayoutInflater inflater;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        inflater = LayoutInflater.from(getApplicationContext());                
        GridView dev_listview =(GridView)findViewById(R.id.dev_listview);
        MyAdapter gAdapter = new MyAdapter (this);          
        dev_listview.setAdapter(gAdapter);
        setContentView(R.id.dev_listview);
    }

MyAdapter.java

 public class MyAdapter extends BaseAdapter {
      private LayoutInflater inflater;
    private Context mcontext;
    private Integer[] mThumbnails = {R.drawable.unpress9};
    public MyAdapter( Context c) 
    { 
       mcontext = c;
    } 
     public int getCount() {
            return mThumbnails.length;
     }
     public Object getItem(int position) {
            return null;
        }

      public long getItemId(int position) {
            return 0;
        }


      @SuppressLint("InflateParams")
    public View getView(int position, View grid, ViewGroup parent) 
      {
          inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(grid != null)
     {
        inflater = LayoutInflater.from(mcontext);
        grid =inflater.inflate(R.layout.item_9,parent);
     }

     return grid;

     }

} 

item_9.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal">
    <LinearLayout
          android:orientation="horizontal">
        <LinearLayout
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text1"/>
            <ImageView
                android:id="@+id/img3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img4" />
            <TextView
                android:id="@+id/text2"/>
            <ProgressBar
                android:id="@+id/bar"  />
            <TextView
                android:id="@+id/text3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img" />
            <TextView
                android:id="@+id/text4" />
            <ProgressBar
                android:id="@+id/br" />
            <TextView
                android:id="@+id/text5"/>
        </LinearLayout>
                <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img94"/>
            <TextView
                android:id="@+id/text6"/>
            <ProgressBar
                android:id="@+id/r" />
            <TextView
                android:id="@+id/text7"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img93" />
            <TextView
                android:id="@+id/text8"/>
            <ProgressBar
                android:id="@+id/ar" />
            <TextView
                android:id="@+id/text9"/>
        </LinearLayout>
        <LinearLayout
           android:orientation="horizontal">
            <ImageView
                android:id="@+id/img22" />
            <TextView
                android:id="@+id/text10"/>
            <ProgressBar
                android:id="@+id/ar13" />
            <TextView
                android:id="@+id/text11"/>
        </LinearLayout>
                <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img6"/>
            <TextView
                android:id="@+id/text12" />
            <ProgressBar
                android:id="@+id/r17" />
            <TextView
                android:id="@+id/text13"/>
        </LinearLayout>       
         <LinearLayout
            android:orientation="horizontal" >
            <LinearLayout
                android:orientation="vertical" >
                <LinearLayout
                   android:orientation="horizontal" >
                    <TextView
                        android:id="@+id/text17"/>
                    <ImageView
                        android:id="@+id/mg"/>
                    <ImageView
                        android:id="@+id/mg2"/>
                    <ImageView
                        android:id="@+id/dev_ctrl_stat_img3"/>
                    <ImageView
                        android:id="@+id/k"/>
                    <ImageView
                        android:id="@+id/img90"/>
                    <ImageView
                        android:id="@+id/img91"/>
                    <TextView
                        android:id="@+id/text19"/>
                                                <TextView
                        android:id="@+id/text20" />                  
                </LinearLayout>
                <LinearLayout
                    android:gravity="center_vertical" />
                    <ImageView                      
                        android:src="@drawable/img9" />
                    <TextView
                        android:gravity="center_vertical" />
                    <TextView
                        android:layout_width="wrap_content"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_weight="4.5" >
                <ImageView
                    android:id="@+id/qry"/>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

想實現的功能是:item.xml全部寫進gridview的一個item中,這個item裡有9行,我下個item調用令一個5行的item5.xml,要保障不同item每行的高度都一樣,如何實現呢,求大神指教!

最佳回答:


 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        inflater = LayoutInflater.from(getApplicationContext());                
        GridView dev_listview =(GridView)findViewById(R.id.dev_listview);
        MyAdapter gAdapter = new MyAdapter (this);          
        dev_listview.setAdapter(gAdapter);
        setContentView(R.id.dev_listview);//怎麼又set布局了,去掉
    }
llx2007
danielinbiti
danielinbiti
llx2007
danielinbiti
llx2007
llx2007
llx2007
llx2007
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved