码迷,mamicode.com
首页 > 其他好文 > 详细

第1章 使用视图UI组件

时间:2015-12-20 20:40:17      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

GridView

      使用方法。同ListView。用于显示列表视图,也是通过Adapter进行数据的适配。

      android:horizontalSpacing="6dp"---横向间距6dp

      android:numColumns="3"---指定列数:三列

      通过setOnItemClickListener为其添加条目点击监听

 

ScrollView

      用于嵌套在其他组件之上,使不能完全显示的组件滚动显示

      内部只能嵌套一个组件,如果要嵌套多个,就用ViewGroup包裹。

     

HorizontalScrollView

      使用方法同ScrollView,只是滚动方向变成了横向,内部也只能放置一个组件

     

Gallery:

      横向滚动的用于展示图片的组件。

      android:spacing="1dp" ---图片间距

      通过Adapter设置要显示的数据

      通过setOnItemClickListener为其添加条目点击监听

      //滚动的时候不触发OnItemSelectedListener

           gallery.setCallbackDuringFling(false);  ------ 滚动状态下,不触发选中监听

           gallery.setOnItemSelectedListener()-----------设置条目选中的监听

     

ImageSwitcher:

      切换展示图片,继承于FrameLayout

      通过setFactory的方法来添加内部的ImageView

 1 switcher.setFactory(new ViewFactory() {
 2     public View makeView() {
 3         ImageView iv = new ImageView(MainActivity.this);
 4         //设置ImageView的宽高
 5         iv.setLayoutParams(new FrameLayout.LayoutParams(
 6                 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
 7                 //设置图片的缩放规则
 8         iv.setScaleType(ScaleType.CENTER_CROP);
 9         return iv;
10     }
11 });
12 switcher.setInAnimation(ani_in);//添加图片切入时的动画效果

通过setonClickListener的方法为其添加点击监听

     

在代码中动态添加View组件:

      首先获取到ViewGroup对象,然后实例化出需要添加的组件,并且设置其属性(宽高、边距等)通过viewgroup.addView(view);

在代码中动态删除View组件:
  viewgroup.removeAllViews();

 

案例代码:

  1 public class MainActivity extends Activity {
  2     int[] images = new int[] { R.drawable.aa, R.drawable.bb, R.drawable.cc,
  3             R.drawable.dd, R.drawable.ee, R.drawable.ff, R.drawable.gg,
  4             R.drawable.aa, R.drawable.bb, R.drawable.cc, R.drawable.dd,
  5             R.drawable.ee, R.drawable.ff, R.drawable.gg, R.drawable.aa,
  6             R.drawable.bb, R.drawable.cc, R.drawable.dd, R.drawable.ee };
  7     private LinearLayout auto;
  8 
  9     @Override
 10     protected void onCreate(Bundle savedInstanceState) {
 11         super.onCreate(savedInstanceState);
 12         setContentView(R.layout.activity_main);
 13         RadioGroup rb = (RadioGroup) this.findViewById(R.id.rg);
 14         auto = (LinearLayout) this.findViewById(R.id.auto_view);
 15         rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
 16 
 17             @Override
 18             public void onCheckedChanged(RadioGroup group, int checkedId) {
 19                 // TODO Auto-generated method stub
 20                 switch (checkedId) {
 21                 case R.id.bt1:
 22                     initGridView();
 23                     break;
 24                 case R.id.bt2:
 25                     initScrollView();
 26                     break;
 27                 case R.id.bt3:
 28                     initGallery();
 29                     break;
 30                 case R.id.bt4:
 31                     initImageSwitcher();
 32                     break;
 33                 }
 34             }
 35         });
 36 
 37         // initGridView();
 38         // initScrollView();
 39         // initGallery();
 40         // initImageSwitcher();
 41 
 42     }
 43 
 44     private void initImageSwitcher() {
 45         // TODO Auto-generated method stub
 46         View v = View.inflate(this, R.layout.switcher_layout, null);
 47         final ImageSwitcher switcher = (ImageSwitcher) v
 48                 .findViewById(R.id.switcher);
 49         switcher.setFactory(new ViewFactory() {
 50 
 51             @Override
 52             public View makeView() {
 53                 // TODO Auto-generated method stub
 54                 ImageView iv = new ImageView(MainActivity.this);
 55                 iv.setLayoutParams(new FrameLayout.LayoutParams(
 56                         LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
 57 
 58                 iv.setScaleType(ScaleType.CENTER_CROP);
 59                 return iv;
 60             }
 61         });
 62         Animation ani_in = AnimationUtils.loadAnimation(this,
 63                 android.R.anim.fade_in);
 64         switcher.setInAnimation(ani_in);
 65         Animation ani_out = AnimationUtils.loadAnimation(this,
 66                 android.R.anim.fade_out);
 67         switcher.setOutAnimation(ani_out);
 68 
 69         switcher.setImageResource(images[0]);
 70         switcher.setOnClickListener(new OnClickListener() {
 71             int i = 0;
 72 
 73             @Override
 74             public void onClick(View v) {
 75                 switcher.setImageResource(images[++i % images.length]);
 76             }
 77         });
 78         auto.removeAllViews();
 79         auto.addView(v);
 80     }
 81 
 82     private void initGallery() {
 83         // TODO Auto-generated method stub
 84         View v = View.inflate(this, R.layout.gallery_layout, null);
 85         Gallery gallery = (Gallery) v.findViewById(R.id.gallery);
 86 
 87         gallery.setAdapter(getAdapter());
 88         //滚动的时候不触发OnItemSelectedListener
 89         gallery.setCallbackDuringFling(false);
 90         gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
 91 
 92             @Override
 93             public void onItemSelected(AdapterView<?> parent, View view,
 94                     int position, long id) {
 95                 Log.w("positon", " " + position);
 96             }
 97 
 98             @Override
 99             public void onNothingSelected(AdapterView<?> parent) {
100 
101             }
102         });
103         auto.removeAllViews();
104         auto.addView(v);
105     }
106 
107     private void initScrollView() {
108         // setContentView(R.layout.scorll_layout);
109         View v = (LinearLayout) View
110                 .inflate(this, R.layout.scorll_layout, null);
111 
112         LinearLayout linear_1 = (LinearLayout) v
113                 .findViewById(R.id.scroll_linear);
114         // LinearLayout linear_1 = (LinearLayout) this
115         // .findViewById(R.id.scroll_linear);
116         for (int i = 0; i < images.length; i++) {
117             LinearLayout ll = (LinearLayout) View.inflate(this, R.layout.image,
118                     null);
119             ImageView iv1 = (ImageView) ll.findViewById(R.id.iviv1);
120             iv1.setImageResource(images[i]);
121 
122             ImageView iv2 = (ImageView) ll.findViewById(R.id.iviv2);
123             if (++i < images.length) {
124 
125                 iv2.setImageResource(images[i]);
126             }
127             linear_1.addView(ll);
128         }
129         auto.removeAllViews();
130         auto.addView(v);
131     }
132 
133     private void initGridView() {
134         // TODO Auto-generated method stub
135         // setContentView(R.layout.scorll_layout);
136         View v = View.inflate(this, R.layout.gridview_layout, null);
137         GridView gv = (GridView) v.findViewById(R.id.grid_view);
138         gv.setAdapter(getAdapter());
139         gv.setOnItemClickListener(new OnItemClickListener() {
140             @Override
141             public void onItemClick(AdapterView<?> parent, View view,
142                     int position, long id) {
143                 // TODO Auto-generated method stub
144                 Toast.makeText(MainActivity.this, "positon  " + position, 1)
145                         .show();
146             }
147         });
148         auto.removeAllViews();
149         auto.addView(v);
150     }
151 
152     public SimpleAdapter getAdapter() {
153         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
154 
155         String[] names = new String[] { "aa", "bb", "cc", "dd", "ee", "ff",
156                 "gg", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "aa", "bb",
157                 "cc", "dd", "ee" };
158         Map<String, Object> map = null;
159         for (int i = 0; i < names.length; i++) {
160             map = new HashMap<String, Object>();
161             map.put("images", images[i]);
162             map.put("names", names[i]);
163             list.add(map);
164         }
165 
166         SimpleAdapter adapter = new SimpleAdapter(this, list,
167                 R.layout.gridview_item, new String[] { "images", "names" },
168                 new int[] { R.id.image_view, R.id.textview });
169 
170         return adapter;
171 
172     }
173 }

 

第1章 使用视图UI组件

标签:

原文地址:http://www.cnblogs.com/starman/p/5061590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!