GridView的用法、自定义控件入门写法和界面切换动画

来源:互联网 发布:琪琪看片软件下载 编辑:程序博客网 时间:2024/06/07 02:04

控件GridView的使用:

GridView是让item以数据表格的形式来显示布局。如果是单列表形式就使用Listview,如果是多行多列网格形式优先是使用GridView,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的。

常用的xml属性:

Android:columnWidth   --设置列的宽度,也就是item的宽度

android:verticalSpacing="10dp"----------垂直边距,就是行间距,九宫格就是指上下格之间的间隙距离

android:horizontalSpacing="10dp"-------水平边距,就是列间距,九宫格就是指左右格之间的间隙距离

android:numColumns=”auto_fit”   列数设置为自动 

android:numColumns=”3” 可以可接受一个 正整数的列数 ,设置只有3列

android:stackFromBottom="true" //设置为true时,你做好的列表就会显示你列表的最下面

用法跟listview是一样的,设置数据适配器,写出item的布局文件。看下面例子:

定义的item的xml文件:

[html] view plain copy
  1. <span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.     <ImageView   
  8.         android:id="@+id/iv_icon"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="@drawable/ic_launcher"  
  12.         />  
  13.       
  14.     <TextView   
  15.         android:id="@+id/tv_title"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:textSize="18sp"  
  19.         android:textColor="#FFB5C5"  
  20.         android:text="模板标题"/>  
  21.   
  22. </LinearLayout></span>  
主界面布局.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <!-- 将对应属性抽取到样式中 -->  
  8.   
  9.     <TextView  
  10.         style="@style/TitleStyle"  
  11.         android:text="功能列表" />  
  12.   
  13. <TextView   
  14.         android:text="啊   五环  你比四环多一环   啊   五环  你比六环少一环  终于有一天    你会修到七环  
  15. 修到七环怎么办   你比五环多两环"  
  16.     android:textColor="#000"  
  17.     android:singleLine="true"  
  18.     android:padding="5dp"  
  19.     android:layout_width="match_parent"  
  20.     android:layout_height="wrap_content"  
  21.     android:ellipsize="marquee"  
  22.     android:focusable="true"  
  23.     android:marqueeRepeatLimit="marquee_forever"  
  24.     android:focusableInTouchMode="true"/>  
  25.       
  26.   
  27.    <!--  <com.itheima.mobilesafe74.view.FocusTextView  
  28.         android:text="啊  五环  你比四环多一环   啊  五环  你比六环少一环  终于有一天 你会修到七环  
  29. 修到七环怎么办   你比五环多两环"  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:ellipsize="marquee"  
  33.         android:focusableInTouchMode="true"  
  34.         android:marqueeRepeatLimit="marquee_forever"  
  35.         android:padding="5dp"  
  36.         android:singleLine="true"  
  37.         android:textColor="#000" >  
  38.     </com.itheima.mobilesafe74.view.FocusTextView> -->  
  39.     <!-- verticalSpacingz.指定垂直方法格与格间距 -->  
  40.     <GridView  
  41.         android:id="@+id/gv_home"  
  42.         android:layout_width="match_parent"  
  43.         android:layout_height="match_parent"  
  44.         android:numColumns="3"  
  45.         android:stackFromBottom="true"  
  46.         android:verticalSpacing="10dp">  
  47.           
  48.     </GridView>  
  49.   
  50. </LinearLayout>
主界面java:以黑马的手机卫士的主界面的九宫格为例
  1. package com.itheima.mobilesafe74.activity;  
  2.   
  3. import com.itheima.mobilesafe74.R;  
  4. import com.itheima.mobilesafe74.utils.ConstantVaule;  
  5. import com.itheima.mobilesafe74.utils.Md5Util;  
  6. import com.itheima.mobilesafe74.utils.SpUtil;  
  7. import com.itheima.mobilesafe74.utils.ToastUtil;  
  8.   
  9. import android.app.Activity;  
  10. import android.app.AlertDialog;  
  11. import android.app.AlertDialog.Builder;  
  12. import android.content.Intent;  
  13. import android.os.Bundle;  
  14. import android.text.TextUtils;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.view.ViewGroup;  
  18. import android.widget.AdapterView;  
  19. import android.widget.AdapterView.OnItemClickListener;  
  20. import android.widget.BaseAdapter;  
  21. import android.widget.Button;  
  22. import android.widget.EditText;  
  23. import android.widget.GridView;  
  24. import android.widget.ImageView;  
  25. import android.widget.TextView;  
  26.   
  27. public class HomeActivity extends Activity {  
  28.     private GridView gv_home;  
  29.     private String[] mTitleStrs;  
  30.     private int[] mDrawableIDs;  
  31.   
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.activity_home);  
  36.   
  37.         initUI();  
  38.         // 初始化数据  
  39.         initDate();  
  40.     }  
  41.   
  42.     private void initDate() {  
  43.         mTitleStrs = new String[] { "手机防盗", "通信卫士", "软件管理", "进程管理","流量统计", "手机杀毒", "缓冲清理", "高级工具", "设置中心" };  
  44.         mDrawableIDs = new int[] { R.drawable.home_safe, R.drawable.home_callmsgsafe,   R.drawable.home_apps,R.drawable.home_taskmanager, R.drawable.home_netmanager, R.drawable.home_trojan,  R.drawable.home_sysoptimize, R.drawable.home_tools, R.drawable.home_settings };  
  1.         // 九宫格控件设置数据适配器  
  2.         gv_home.setAdapter(new MyAdapter());  
  3.         // 注册九宫格的单个点击事件  
  4.         gv_home.setOnItemClickListener(new OnItemClickListener() {  
  5.   
  6.             @Override  
  7.             // position点中条目的索引  
  8.             public void onItemClick(AdapterView<?> parent, View view,int position, long id) {  
  9.                 switch (position) {  
  10.                 case 0:  
  11.                     // 开启对话框  
  12.                     showDialog();  
  13.                     break;  
  14.                 case 8:  
  15.         Intent intent = new Intent(getApplicationContext(), SettingActivity.class);  
  16.         startActivity(intent);  
  17.         break;  
  18.   
  19.                 }  
  20.             }  
  21.   
  22.         });  
  23.     } 

  24.     class MyAdapter extends BaseAdapter {  
  25.   
  26.         @Override  
  27.         public int getCount() {  
  28.             // 条目的总数 ,为九宫格格数  
  29.             return 9;  
  30.         }  
  31.   
  32.         @Override  
  33.         public Object getItem(int position) {  
  34.             return mTitleStrs[position];  
  35.         }  
  36.   
  37.         @Override  
  38.         public long getItemId(int position) {  
  39.             return position;  
  40.         }  
  41.   
  42.         @Override  
  43.         public View getView(int position, View convertView, ViewGroup parent) {  
  44.             View view = View.inflate(getApplicationContext(),


  1. R.layout.gridview_item, null);  
  2.             TextView tv_title = (TextView) view.findViewById(R.id.tv_title);  
  3.             ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_icon);  
  4.   
  5.             tv_title.setText(mTitleStrs[position]);  
  6.             iv_icon.setBackgroundResource(mDrawableIDs[position]);  
  7.   
  8.             return view;  
  9.         }  
  10.   
  11.     }  
  12.   
  13.     private void initUI() {  
  14.         gv_home = (GridView) findViewById(R.id.gv_home);  
  15.   
  16.     }  
  17. }

效果图:


设置了适配器,跟listview用法上没有什么区别。


自定义控件入门

对于产品在android标准控件库中没有满足要求的,或者要根据一些判断来显示的控件,某些布局会被重复的利用,同一个布局的XML代码块会被重复的复制黏贴多次,则需要自己去自定义一个。

自定义控件的步骤:

0,在工程目录res/values目录下,创建一个arrts.xml文件,自定义属性

1,写一个自定义控件类,这个类就是你的自定义控件的实现:

2,复写其构造方法

3,在布局文件中定义出 引用自定义控件的方法

4,实现自定义的逻辑和需求


例子:

第0步:在工程目录res/values目录下,创建一个arrts.xml文件,自定义属性

styleable name 为要继承于控件的类名


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <declare-styleable name="com.itheima.mobilesafe74.view.SettingItemView">  
  5.         <attr name="destitle" format="string" />  
  6.         <attr name="desoff" format="string" />  
  7.         <attr name="deson" format="string" />  
  8.          <attr name="data" format="string" />  
  9.     </declare-styleable>  
  10.   
  11. </resources>
1步、第2步,第4步一起:

先:定义好了属性集了,接下来我们就需要定义一个Java类,来渲染这段布局,解析这个属性集,并且对象提供修改控件状态的方法,已达到复用的效果。

再:继承RelativeLayout(或者其他ViewGroup子类),复写其构造方法,在构造方法中先渲染布局的视图,然后读取属性集的属性,将默认显示的属性显示到布局上的子控件上即可。

加上:自己要实现的逻辑和需求


  1. package com.itheima.mobilesafe74.view;  
  2.   
  3. import com.itheima.mobilesafe74.R;  
  4.   
  5. import android.content.Context;  
  6. import android.util.AttributeSet;  
  7. import android.util.Log;  
  8. import android.view.View;  
  9. import android.widget.CheckBox;  
  10. import android.widget.RelativeLayout;  
  11. import android.widget.TextView;  
  12. import android.widget.Toast;  
  13.   
  14. public class SettingItemView extends RelativeLayout {  
  15.     private CheckBox cb_box;  
  16.     private TextView tv_des;  
  17.     private String namespace = "http://schemas.android.com/apk/res/com.itheima.mobilesafe74";  
  18.     private String namespace2;  
  19.     private String mDestitle;  
  20.     private String mDesoff;  
  21.     private String mDeson;  
  22.     private TextView tv_title;  
  23.   
  24.     public SettingItemView(Context context, AttributeSet attrs) {  
  25.         this(context,attrs,0);  
  26.     }  
  27.   
  28.     public SettingItemView(Context context) {  
  29.         this(context, null);  
  30.     }  
  31.   
  32.   
  33.     public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {  
  34.         super(context, attrs, defStyleAttr);  
  35.         //xml转为view对象,将设置的条目转换成view对象  
  36.         //this 指SettingItemView对象 直接添加到当前SettingItemView对应view上再放到SettingActivity上面  
  37.         View.inflate(context, R.layout.setting_item_view, this);  
  38.           
  39.         tv_title = (TextView) findViewById(R.id.tv_title);  
  40.         tv_des = (TextView) findViewById(R.id.tv_des);  
  41.         cb_box = (CheckBox) findViewById(R.id.cb_box);  
  42.           
  43.         //获取自定义以及原生属性的操作,写在此处 AttributeSet attrs中获取  
  44.         initAttrs(attrs);  
  45.         //获取布局文件中定义的字符串,赋值给自定义组合控件的标题  
  46.         tv_title.setText(mDestitle);  
  47.     }  
  48.       
  49.     /**  
  50.      * 返回属性集合中定义属性属性值  
  51.      * @param attrs  构造方法中维护好的属性集合  
  52.      */  
  53.     private void initAttrs(AttributeSet attrs) {  
  54.         int attributeCount = attrs.getAttributeCount();  
  55.         namespace2 = "http://schemas.android.com/apk/res/com.itheima.mobilesafe74";  
  56.         mDestitle = attrs.getAttributeValue(namespace2, "destitle");  
  57.         mDesoff = attrs.getAttributeValue(namespace2, "desoff");  
  58.         mDeson = attrs.getAttributeValue(namespace2, "deson");  
  59.     }  
  60.   
  61.     /**  
  62.      * @return   
  63.      * 返回当前SettingItemView 是否选择状态 true开启  
  64.      */  
  65.     public  boolean isCheck(){  
  66.         return cb_box.isChecked();  
  67.     }  
  68.       
  69.     /**  
  70.      * @param isCheck  是否作为开启的变量,有点击事件确定  
  71.      */  
  72.     public void setCheck(boolean isCheck) {  
  73.         //当前条目选择的过程中,cb_box的状态也跟随isCheck 变化  
  74.         cb_box.setChecked(isCheck);  
  75.         if(isCheck){  
  76.             //开启,  
  77.             tv_des.setText(mDeson);  
  78.         }else{  
  79.             //关闭  
  80.             tv_des.setText(mDesoff);  
  81.         }  
  82.     }  
  83.   
  84.       
  85. }


必须:一定要在Activity的布局文件中定义出来这个自定义组合控件。布局节点上必须要加上命名空间
  1. xmlns:mobilesafe="http://schemas.android.com/apk/res/com.itheima.mobilesafe74"  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:mobilesafe="http://schemas.android.com/apk/res/com.itheima.mobilesafe74"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.       
  8.     <TextView   
  9.         style="@style/TitleStyle"  
  10.         android:text="设置中心"/>  
  11.       
  12.    <!--  <RelativeLayout   
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         >  
  16.         <TextView   
  17.             android:id="@+id/tv_title"  
  18.             android:text="自动更新设置"  
  19.             android:textColor="#000"  
  20.             android:textSize="18sp"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"/>  
  23.         <TextView   
  24.             android:id="@+id/tv_des"  
  25.             android:text="自动更新关闭"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:textSize="18sp"  
  29.             android:textColor="#000"  
  30.             android:layout_below="@id/tv_title"/>  
  31.   
  32.         <CheckBox  
  33.             android:id="@+id/cb_box"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:layout_alignParentRight="true"  
  37.             android:layout_centerVertical="true" />  
  38.         <View  
  39.             android:background="#000"  
  40.             android:layout_below="@id/tv_des"   
  41.             android:layout_width="match_parent"  
  42.             android:layout_height="1dp"  
  43.             />  
  44.   
  45.     </RelativeLayout> -->  
  46.       
  47.     <com.itheima.mobilesafe74.view.SettingItemView  
  48.         android:id="@+id/siv_update"  
  49.         android:layout_width="match_parent"  
  50.         android:layout_height="wrap_content"  
  51.         mobilesafe:destitle="自动更新设置"  
  52.         mobilesafe:desoff="自动更新关闭"  
  53.         mobilesafe:deson="自动更新开启">  
  54.     </com.itheima.mobilesafe74.view.SettingItemView>  
  55.     <!--SettingView需要在构建布局的时间知道titile和des字符串内容  -->  
  56.     <!--自定义属性  相比上面和下面的代码量  -->  
  57.   
  58. </LinearLayout>


实现的效果:




根据点击事件来更新布局,和逻辑要求!



界面切换动画:当界面跳转的时候,界面之间的切换动画效果。例子:动画平移的效果

动画平移要先了解平移的原理,平移其实可以看成界面原点的移动。看下图:

这时 上一页进来的动画效果:


下面是   下一页动画效果图:


所以在界面的移动过程中,平移动画的坐标变化如下:

上一页移入动画(-屏幕宽度,y)------>(0,y)

上一页移出动画(0,y)-------------->(屏幕宽度,y)

下一页移入动画 (屏幕宽度,y)-------------->(0,y)

下一页移出动画(0,y)-------------->(-屏幕宽度,y)


例子:

动画效果是要在res文件中创建一个anim的文件,在该文件中写动画效果的xml:

next_in_anim  这是:下一页新界面进来的时候动画:

  1. <span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 100%p  屏幕的宽度大小值 -->  
  3. <translate  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.      android:fromXDelta="100%p"  
  6.      android:toXDelta="0"  
  7.      android:duration="500"  
  8.      >  
  9.   
  10. </translate></span> 
next_out_anim 这是: 下一页旧的界面消失的动画:
  1. <span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:fromXDelta="0"  
  4.      android:toXDelta="-100%p"  
  5.      android:duration="500"  
  6.      >  
  7. </translate></span><span style="font-size:18px;">  
  8. </span>  


pre_in_anim  这是:上一页新的界面进来的动画
  1. <span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:fromXDelta="-100%p"  
  4.      android:toXDelta="0"  
  5.      android:duration="500"  
  6.      >  
  7. </translate></span>  


pre_out_anim  这是:上一页旧的界面消失的动画
  1. <span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:fromXDelta="0"  
  4.      android:toXDelta="100%p"  
  5.      android:duration="500"  
  6.      >  
  7. </translate></span><span style="font-size:18px;">  
  8. </span>  
注:android:duration="500" 是动画的效果时间 (毫秒)


写完动画后,在程序的调整界面是加上动画:

跳转到上一页

  1. public void prePage(View v) {  
  2.         Intent intent = new Intent(getApplicationContext(), Setup1Activity.class);  
  3.         startActivity(intent);  
  4.         finish();  
  5.         overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);  
  6.     }  
跳转到下一页
  1. public void nextPage(View v) {  
  2.           
  3.         Intent intent = new Intent(getApplicationContext(), Setup3Activity.class);  
  4.         startActivity(intent);  
  5.         finish();  
  6.           
  7.         overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);  
  8.     }  

也有的效果是直接跳转的,取消了动画效果,系统自动的也消除了。

跳转到上一页

  1. <span style="font-size:18px;">public void prePage(View v) {  
  2.         Intent intent = new Intent(getApplicationContext(), Setup1Activity.class);  
  3.         startActivity(intent);  
  4.         finish();  
  5.         overridePendingTransition(0,0);  
  6.     }</span>  
跳转到下一页

  1. public void nextPage(View v) {  
  2.           
  3.         Intent intent = new Intent(getApplicationContext(), Setup3Activity.class);  
  4.         startActivity(intent);  
  5.         finish();  
  6.           
  7.         overridePendingTransition(0, 0);  
  8.     } 


阅读全文
0 0
原创粉丝点击