主界面采用GridView实现:
布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f2f0eb"
>
<TextView
android:id="@+id/title"
android:background="#2FD5B9"
android:layout_width="match_parent"
android:layout_height="46dp"
android:gravity="center"
android:text="主页"
android:textColor="#ffffff"
android:textSize="20sp" />
<GridView
android:layout_marginTop="15dp"
android:id="@+id/list_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:verticalSpacing="5dp"
></GridView>
</LinearLayout>
每个单独的item的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:layout_marginTop="10dip"
android:id="@+id/iv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机防盗"
android:textSize="16sp"
android:textColor="#000000"
/>
</LinearLayout>
逻辑代码如下:
package com.ustc.mobilemanager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class HomeActivity extends Activity {
private GridView list_home;
private MyAdapter adapter;
private static String[] names =
{
"手机防盗","通讯卫士","软件管理",
"进程管理","流量统计","手机杀毒",
"缓存清理","高级工具","设置中心"
};
private static int[] ids = {
R.drawable.shoujifangdao,R.drawable.tongxunweishi,R.drawable.ruanjianguanli,
R.drawable.jinchengguanli,R.drawable.liuliangtongji,R.drawable.shoujishadu,
R.drawable.huancunqingli,R.drawable.gaojigongju,R.drawable.shezhizhongxin
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home);
list_home = (GridView) findViewById(R.id.list_home);
adapter = new MyAdapter();
list_home.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return names.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(HomeActivity.this, R.layout.list_item_home, null);
ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
tv_item.setText(names[position]);
iv_item.setImageResource(ids[position]);
return view;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
}
自定义可以滚动的TextView:
package com.ustc.mobilemanager.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;
public class FocusedTextView extends TextView {
public FocusedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public FocusedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FocusedTextView(Context context) {
super(context);
}
/**
* 当前并没有view 只是欺骗了Android系统
*
*/
@Override
@ExportedProperty(category = "focus")
public boolean isFocused() {
return true;
}
}
重写isFocus方法,让他一直返回true。然后在布局中添加如下的代码:
<com.ustc.mobilemanager.ui.FocusedTextView
android:focusableInTouchMode="true"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="16sp"
android:textColor="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="最新的手机管家,快去下载,下载送iphone6哦,机不可失,失不再来!"
/>