<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 主界面布局(知識點:GridView) #### mainscreen.xml: ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background" android:orientation="vertical" > <GridView android:id="@+id/gv_main" android:layout_width="match_parent" android:layout_height="match_parent" android:horizontalSpacing="10dip" android:verticalSpacing="20dip" android:numColumns="3" > </GridView> </LinearLayout> ~~~ android:background="@color/background"--設置背景顏色,此處采用顏色資源文件(/mobilesafe/res/values/color.xml) #### color.xml: ~~~ <?xml version="1.0" encoding="utf-8"?> <resources> <color name="background">#ff404040</color> <color name="textcolor">#ffd0d0d0</color> </resources> ~~~ #### GridView : android:numColumns="3"-----列數設置為3列 android:horizontalSpacing="10dip"-----兩列之間的邊距 android:verticalSpacing="20dip"-----兩行之間的邊距 ### 數據適配MainUIAdapter(知識點:[LayoutInflater](http://blog.csdn.net/bruce_6/article/details/40785969),BaseAdapter ) MainUIAdapter : ~~~ package com.liuhao.mobilesafe.adapter; import com.liuhao.mobilesafe.R; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MainUIAdapter extends BaseAdapter { private static final String TAG = "MainUIAdapter"; private Context context;//用于接收傳遞過來的Context對象 private LayoutInflater inflater; private static ImageView iv_icon; private static TextView tv_name; private SharedPreferences sp; public MainUIAdapter(Context context) { this.context = context; inflater = LayoutInflater.from(context); sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } // 數據源,子條目的文本內容 private static String[] names = { "手機防盜", "通訊衛士", "軟件管理", "任務管理", "上網管理", "手機殺毒", "系統優化", "高級工具", "設置中心" }; // 數據源,子條目的對應圖片 private static int[] icons = { R.drawable.widget05, R.drawable.widget02, R.drawable.widget01, R.drawable.widget07, R.drawable.widget05, R.drawable.widget04, R.drawable.widget06, R.drawable.widget03, R.drawable.widget08 }; /** * How many items are in the data set represented by this Adapter. * 在此適配器中所代表的數據集中的條目數 */ @Override public int getCount() { return names.length; } /** * Get the data item associated with the specified position in the data set. * 獲取數據集中與指定索引對應的數據項 */ @Override public Object getItem(int position) { return position; } /** * Get the row id associated with the specified position in the list. * 取在列表中與指定索引對應的行id */ @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //getView()方法被調用了多少次? //9次? //GridView 控件bug // 使用靜態變量引用來減少內存中申請的引用的個數 Log.i(TAG, "getView" + position); // 得到布局文件對應的View對象 // inflate()方法一般接收兩個參數,第一個參數就是要加載的布局id, // 第二個參數是指給該布局的外部再嵌套一層父布局,如果不需要就直接傳null。 View view = inflater.inflate(R.layout.mainscreen_item, null); iv_icon = (ImageView) view.findViewById(R.id.iv_main_icon); tv_name = (TextView) view.findViewById(R.id.tv_main_name); iv_icon.setImageResource(icons[position]); tv_name.setText(names[position]); if(position == 0){ // 從SharedPreferences獲取條目的文本內容 String name = sp.getString("lost_name", null); if(!"".equals(name) && null != name){ tv_name.setText(name); } } return view; } } ~~~ ### 子條目的布局: mainscreen_item.xml: ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dip" android:layout_height="100dip" android:orientation="vertical" android:gravity="center_horizontal" android:background="@drawable/item_background" > <ImageView android:id="@+id/iv_main_icon" android:layout_width="60dip" android:layout_height="60dip" android:src="@drawable/ic_launcher" android:scaleType="fitXY" /> <TextView android:id="@+id/tv_main_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@color/textcolor" android:text="程序功能" /> </LinearLayout> ~~~ ### 為每個條目添加背景圖(知識點:android:shape) item_background.xml: ~~~ <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 邊框 --> <stroke android:width="1dip" android:color="#ff202020" /> <!-- 指定邊角 --> <corners android:radius="2dip" /> <!-- 指定固定顏色 --> <solid android:color="@color/background" /> <!-- 漸變色 --> <gradient /> </shape> ~~~ MainActivity中使用 ~~~ gv_main = (GridView) this.findViewById(R.id.gv_main); adapter = new MainUIAdapter(this); gv_main.setAdapter(adapter); ~~~ 效果: [![Screenshot_2014-11-04-14-46-21](https://box.kancloud.cn/2016-02-18_56c5a9560ecdd.jpg "Screenshot_2014-11-04-14-46-21")](http://img.blog.csdn.net/20141104150011421) ### GridView中的getView()方法被調用了多少次?9次(9個條目)? 每個條目會多次調用getView()方法; 原因: GridView的item的layout中android:layout_height定義為wrap_content , 繪制item高度時系統并不知道item應該繪制多高,它會先取一條來試探以確定item繪制的具體高度,這樣就導致多調用了一次getView方法。在滑動、長按等GridView需要重繪的情況下,getView的調用次數可能會不止多一次。 解決方法1: 由于多次調用getView()方法,每次都會重新生成一個iv_icon ,tv_name控件對象,從而產生多個對象實例,造成空間浪費。因此,一個簡單的優化方法就是,將二者設置為類靜態變量: private static ImageView iv_icon; private static TextView tv_name; 使用靜態變量引用來減少內存中申請的引用的個數,這樣可以在一定程度上提高效率。 后續還會有其他優化方法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看