易网时代-编程资源站
Welcome
微信登录
首页
/
操作系统
/
Linux
/
Android 软件管理器的开发
一、布局
分三块:1. 2. 3. 第一部分是一张图(ImageView) 和 几个字(TextView)第二部分是列表(ListVIew)第三部分是三个按钮 具体怎么去放到合理的位置就不具体说了。自己慢慢试,这样才能熟练。(提示: 可以用相对布局àRelativeLayout 来整体布局这三块)。 可以参考项目中的show.xml:已经写好了注释
<?xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<RelativeLayout
xmlns:Android
=
"http://schemas.android.com/apk/res/android"
xmlns:umadsdk
=
"http://schemas.android.com/apk/res/stane.appExplorer"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:background
=
"#313849"
>
<!-- 1. 单位有dip、px,具体区别自己百度。
2. 字体的单位用 sp
3. gravity 是自己相对与父控件的位置;Layout_gravity是自己的子控件相对与自己的位置
4. orientation: Layout中的控件是水平 排列还是竖直
其它的可以参看文档,文档中写的很详细,也很简单--
>
<LinearLayout
android:layout_height
=
"30dip"
android:layout_width
=
"fill_parent"
android:orientation
=
"horizontal"
android:gravity
=
"center_vertical"
android:paddingLeft
=
"5dip"
android:background
=
"@drawable/top_bg"
>
<ImageView
android:layout_width
=
"18dip"
android:layout_height
=
"18dip"
android:src
=
"@drawable/manage"
/>
<TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textColor
=
"#000"
android:textSize
=
"14sp"
android:text
=
"@string/app_type"
/>
</LinearLayout>
<LinearLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:layout_marginTop
=
"28dip"
android:layout_marginBottom
=
"100dip"
android:layout_marginLeft
=
"5dip"
android:layout_marginRight
=
"5dip"
>
<ListView
android:id
=
"@+id/lv_apps"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:listSelector
=
"@drawable/choose_listview"
android:visibility
=
"gone"
/>
</LinearLayout>
<RelativeLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"50dip"
android:layout_alignParentBottom
=
"true"
android:background
=
"@drawable/bottom_bg"
>
<ImageButton
android:id
=
"@+id/ib_change_view"
android:layout_alignParentLeft
=
"true"
android:layout_width
=
"70dip"
android:layout_height
=
"45dip"
android:layout_marginRight
=
"5dip"
android:layout_marginTop
=
"3dip"
android:src
=
"@drawable/list"
/>
<ImageButton
android:id
=
"@+id/ib_change_category"
android:layout_alignParentRight
=
"true"
android:layout_width
=
"70dip"
android:layout_height
=
"45dip"
android:layout_marginRight
=
"5dip"
android:layout_marginTop
=
"3dip"
android:src
=
"@drawable/all"
/>
<Button
android:id
=
"@+id/ib_shop"
android:layout_centerInParent
=
"true"
android:layout_width
=
"70dip"
android:layout_height
=
"50dip"
android:text
=
"淘软件"
android:background
=
"@drawable/button_install_selector"
/>
</RelativeLayout>
</RelativeLayout>
二、 ListView 关于ListView 要用 适配器来填充内容。 适配器有好几种: SimpleAdapter、ArrayAdapter等。 不过项目中经常要自己继承基类BaseAdapter 。
package
com.stone.app;
import
android.content.Context;
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
ListViewAdapter
extends
BaseAdapter {
LayoutInflater inflater;
Context context;
public
ListViewAdapter(Context context) {
// TODO Auto-generated constructor stub
this
.context = context;
inflater = LayoutInflater.from(context);
}
/**
* getCount()中的返回值决定了ListView有的列数
*/
@Override
public
int
getCount() {
// TODO Auto-generated method stub
return
10
;
}
/**
* 可以返回null, 也可以返回其它的数据。
*/
@Override
public
Object getItem(
int
position) {
// TODO Auto-generated method stub
return
null
;
}
@Override
public
long
getItemId(
int
position) {
// TODO Auto-generated method stub
return
0
;
}
/**
* 这个是最重要的方法。 返回值就是列表中第position列要显示的内容
* position: 当前的View位于第几列
* convertView: 当前的的列上 要显示的内容
*/
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.lv_item,
null
);
// lv_item.xml 就是每一列要显示的内容
ImageView imageView = (ImageView) view.findViewById(R.id.lv_icon);
TextView tv_appname = (TextView) view.findViewById(R.id.lv_item_appname);
TextView tv_package = (TextView) view.findViewById(R.id.lv_item_packagename);
imageView.setBackgroundResource(R.drawable.icon);
tv_appname.setText(
"应用的名称"
);
tv_package.setText(context.getResources().getString(R.string.packageName));
return
view;
}
}
再来看上面的代码, 在getView中,每列都会new 一个view,然后去填充相应的数据, 这就需要注意了,因为getView在每项显示的时候就会调用(包括滚动重新显示)就会调用。 这样就会浪费手机的资源。其实每一项的view只需要填充一次。下面的getView优化后的代码
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder view =
null
;
if
(convertView ==
null
) {
view =
new
Holder();
convertView = inflater.inflate(R.layout.lv_item,
null
);
view.imageView = (ImageView) convertView
.findViewById(R.id.lv_icon);
view.tv_appname = (TextView) convertView
.findViewById(R.id.lv_item_appname);
view.tv_package = (TextView) convertView
.findViewById(R.id.lv_item_packagename);
convertView.setTag(view);
//通过setTag把该view保存起来。
}
else
{
view = (Holder) convertView.getTag();
//convertView不为空是,直接拿到保存的view
}
view.imageView.setBackgroundResource(R.drawable.icon);
view.tv_appname.setText(
"应用的名称"
);
view.tv_package.setText(context.getResources().getString(R.string.packageName));
return
convertView;
}
Holder是一个内部类:
class
Holder {
ImageView imageView;
TextView tv_appname;
TextView tv_package;
TextView textView2;
ImageView imageView2;
}
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图