Android App----Mr Expense 之主界面

这篇文章放在草稿箱很多天了,知道现在才发出来,主要是我的主界面和Listview相关甚大,但是博主对listview用法始终掌握不好,这几天有空的时候就看了好多文章,总结一下listview,感觉很强大的酱紫。

-------------------------------------------


先上个被我抛弃的主界面:


main.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:orientation="vertical" >
	<ListView android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:divider="@null"
        android:cacheColorHint="@android:color/transparent" />     
</LinearLayout>

listview.xml:

<?xml version="1.0" encoding="utf-8"?>  
  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" >  
    <ImageView android:id="@+id/image"  
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_centerVertical="true" 
        android:layout_alignParentTop="true" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentLeft="true" 
        android:adjustViewBounds="true"  
        android:padding="2dp" />  
    <TextView android:id="@+id/title"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@id/image"  
        android:layout_alignParentTop="true"  
        android:layout_alignWithParentIfMissing="true"   
        android:textSize="30dp" />
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/title"
        android:layout_toRightOf="@id/image"
        android:ellipsize="marquee"
        android:gravity="bottom"
        android:singleLine="true"
        android:textSize="19dp" />
  
</RelativeLayout> 

public class Main extends ListActivity {  
    private String[] mListTitle = { "food", "transport", "utilities", "healthcare","clothes","entertainment","others"};  
    private String[] mListStr = {  "今天你吃饭了吗", "今天你坐公交车了没", "今天你修爱疯了木", "今天你打针了么", 
    		"今天你买球鞋了嘛","今天你K歌了麽","也许还有一些东西-。=" };  
    private Object[] mListImage={R.drawable.shiwu,R.drawable.jiaotong,R.drawable.shebei,
    		R.drawable.yiliao,R.drawable.yifu,R.drawable.yule,R.drawable.zawu};
    ListView mListView = null;  
    ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
    requestWindowFeature(Window.FEATURE_NO_TITLE);
	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
	WindowManager.LayoutParams.FLAG_FULLSCREEN);
	setContentView(R.layout.main);
    mListView = getListView();  
        
    int lengh = mListTitle.length;  
    for(int i =0; i < lengh; i++) {  
        Map<String,Object> item = new HashMap<String,Object>(); 
        item.put("image", mListImage[i]);  
        item.put("title", mListTitle[i]);  
        item.put("text", mListStr[i]);
        mData.add(item);   
    }  
    SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.listitem,  
        new String[]{"image","title","text","background"},new int[]{R.id.image,R.id.title,R.id.text});  
        setListAdapter(adapter);  
    mListView.setOnItemClickListener(new OnItemClickListener() {  
        @Override  
        public void onItemClick(AdapterView<?> adapterView,View view, int position,  
            long id) {  
        Toast.makeText(Main.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show();  
        }  
    });  
    super.onCreate(savedInstanceState);  
    }  
} 

说明:

0、主界面很简单,就一个Listview,然后每个item有3个view,好白有木有!

1、android:divider="@null"这句话会把item之间的黑线去掉,哎没去掉之前更是不忍直视

2、一开始我的Listview充满不了屏幕,然后上网问啊,有个人跟我说layout设置vertical,然后权重设1,于是如代码所示,

android:layout_weight="1",但其实是没用的,我之所以没去掉,主要是看到能让我想起它的其他作用,权重可以分配位置的大小,但是也不是那么想当然的分配,有篇文章深入探讨了:ht@tp://mobile.51cto.com/abased-375428.htm

3、android:cacheColorHint="@android:color/transparent" 去底色,在这里可用可不用

4、Listview用的RelativeLayout,发现比LinearLayout复杂太多,常用属性:ht@tp://blog.csdn.net/notenlife/article/details/7256363

5、android:layout_centerVertical="true"让图片在imageview中居中

6、为了让Listview充满屏幕,只能调节字体的大小了,不知道还有没有其他简单高效的方法

7、android:gravity="bottom" text在最下面体现

8、android:ellipsize="marquee"  android:singleLine="true"单行 跑马,当然在这里是多余的。另外,想要跑马灯,设置focus,详见:ht@tp://www.cnblogs.com/Gaojiecai/archive/2013/06/18/3142783.html

9、下面来说说Listview

Listview和数据关联要用到adapter,常见的ArrayAdapter,SimpleAdapter,还有继承BaseAdapter

这个主界面是用的SimpleAdapter(this,data,layout,string,int)

getListView()是只有继承ListActivity才有的


但是这个主界面越看越不爽,有木有,虽然简约是我的风格,but做人做事那是两码子事,果断throw IT away。

我接下来就想给每个item都上个色,五彩斑斓的多好,然后其他地方也微调下。差不多就酱紫。

于是我在main.xml加了android:layout_background="@+id/background",然后在Activity中直接把颜色put进item中,然后设置下adapter,想当然以为Listview能五颜六色了,发现报错了。。。看来这个只是用于整个Listview的背景色。

然后呢,我就用了Listview的BaseAdapter,自定义总可以了吧。

public class Main extends ListActivity {
	
	private String[] mListTitle = { "food", "transport", "utilities", "healthcare","clothes","entertainment","others"};  
    private String[] mListStr = {  "今天你吃饭了吗", "今天你坐公交车了没", "今天你修爱疯了木", "今天你打针了么", 
    		"今天你买球鞋了嘛","今天你K歌了麽","也许还有一些东西 -。=" };  
    private int[] mListImage={R.drawable.shiwu,R.drawable.jiaotong,R.drawable.shebei,
    		R.drawable.yiliao,R.drawable.yifu,R.drawable.yule,R.drawable.zawu};
    private int[] mListColor=new int []{0xffff0033,0xff66cccc,0xff3333cc,0xff66cc33,0xffffcc33,
    		0xffff9900,0xffcc6600};
    
    ListView mListView = null;
    MyListAdapter myAdapter = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    	WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	
    	setContentView(R.layout.main);
        
    	mListView = getListView(); 
	    myAdapter = new MyListAdapter(this);
	    setListAdapter(myAdapter);
	    mListView.setOnItemClickListener(new OnItemClickListener() {
	    	@Override
	    	public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) {
	    		View v=adapterView.getChildAt(position);
	    		Toast.makeText(Main.this,"您选择了" + mListTitle[position], Toast.LENGTH_LONG).show();
	        }
	    });
	    super.onCreate(savedInstanceState);
	    }

    class MyListAdapter extends BaseAdapter {
    	private Context mContext;
    	public MyListAdapter(Context context) { 
    		mContext = context;
    		}

    	public int getCount() {   
    		return mListStr.length;
	    }

	    @Override
	    public boolean areAllItemsEnabled() {
	        return false;
	    }

	    public Object getItem(int position) {
	        return position;
	    }

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

	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView image = null;
	        TextView title = null;
	        TextView text = null;
	        
	        if (convertView == null) {
	        	convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem, null);
		        image = (ImageView) convertView.findViewById(R.id.image);
		        title =(TextView) convertView.findViewById(R.id.title);
		        text= (TextView) convertView.findViewById(R.id.text);
	            }  
	        int colorPos = position % mListColor.length;
	        convertView.setBackgroundColor(mListColor[colorPos]);
	        image.setImageResource(mListImage[position]);
	        title.setText(mListTitle[position]);
	        text.setText(mListStr[position]);
	           
	        return convertView;
	        }
	    }
    }

效果图:


既然走的是“色”的路线,那么欢迎界面也该搞得“色”一点,这样跳跃才不会太大,O(∩_∩)O~

OK,到此主界面先这样,还木有数据的处理呢。

作业可以先handle了,接下来该去看看怎么搞数据了。


-------------------------

4.23 nju

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值