标签:
学习了有一个多星期的android了,今天跟着视频来个小案例练习下,在这里记录下:
开发软件 android studio
第一步:先在activity_main.xml文件中写布局代码,这里用一个listview来展示每一条新闻
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv_news"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
知识点补充:
fill_parent
设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。
wrap_content
设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。
match_parent
level8开始fill_parent替换为match_parent,match_parent和fill_parent是一个意思 .两个参数意思一样
tools:context=".MainActivity">
表示,该布局配置文件的使用环境上下文为
MainActivity
这个类
第二步:为每一个新闻项自定义一个布局文件
<?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="wrap_content"
android:padding="5dp"
>
<com.loopj.android.image.SmartImageView
android:id="@+id/siv_listvie_item_icon"
android:layout_width="100dip"
android:layout_height="60dip"
android:src="@drawable/a2"
/>
<TextView
android:id="@+id/tv_listview_item_title"
android:layout_toRightOf="@id/siv_listvie_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="3Q大战宣判:腾讯获赔500万"
android:textColor="@android:color/black"
android:textSize="17sp"/>
<TextView
android:id="@+id/tv_listview_item_detail"
android:layout_alignLeft="@id/tv_listview_item_title"
android:layout_below="@id/tv_listview_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sdjsahdkjsahdkjsadhjksahdkj"
android:textColor="@android:color/darker_gray"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_listview_item_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="668跟帖"
android:textColor="@android:color/darker_gray"
android:textSize="14sp"/>
</RelativeLayout>
第三步:开始编写逻辑文件
com.zaizai.newsandroid.view.LayoutInflaterandroid.view.Menuandroid.view.MenuItemandroid.view.ViewGroupandroid.widget.BaseAdapterorg.apache.http.HttpResponseorg.apache.http.client.HttpClientorg.xmlpull.v1.XmlPullParserjava.io.InputStreamjava.util.List{
= = = = List<> = = Handler() {
(msg) {
(msg.) {
: = (List<>) msg.myAdaper = ().setAdapter(myAdaper):.(..).show():
}
.handleMessage(msg)}
}(savedInstanceState) {
.onCreate(savedInstanceState)setContentView(.)init()}
() {
= () findViewById(.)Thread(Runnable() {
() {
List<> newInfList = getNewsFromInternet()message = Message()(newInfList != ) {
message.= message.= newInfList} {
message.= }
//通过sendMessage()函数将数据传给主线程
.sendMessage(message)}
}).start()}
List<> () {
HttpClient httpclient = {
httpclient = DefaultHttpClient()get = HttpGet()HttpResponse httpResponse = httpclient.execute(get)statusCode = httpResponse.getStatusLine().getStatusCode()(statusCode == ) {
InputStream inputStream = httpResponse.getEntity().getContent()List<> newsInfos = getNewsListFromInputStream(inputStream)newsInfos} {
.(+ statusCode)}
} (e) {
e.printStackTrace()} (e) {
e.printStackTrace()} {
(httpclient != ) {
httpclient.getConnectionManager().shutdown()}
}
}
List<> (InputStream is) {
factory = .()XmlPullParser parser = factory.newPullParser()parser.setInput(is)eventType = parser.getEventType()List<> infoList = newsInfo = (eventType != XmlPullParser.) {
tagName = parser.getName()(eventType) {
XmlPullParser.: (.equals(tagName)) {
infoList = ArrayList<>()} (.equals(tagName)) {
newsInfo = ()} (.equals(tagName) && newsInfo != ) {
newsInfo.setTitle(parser.nextText())} (.equals(tagName) && newsInfo != ) {
newsInfo.setDetail(parser.nextText())} (.equals(tagName) && newsInfo != ) {
newsInfo.setComment(.(parser.nextText()))} (.equals(tagName) && newsInfo != ) {
newsInfo.setImageUrl(parser.nextText())}
XmlPullParser.:(.equals(tagName) && newsInfo != ) {
infoList.add(newsInfo)}
:
}
eventType = parser.next()}
infoList}
BaseAdapter {
() {
.size()}
(positionconvertViewViewGroup parent) {
view = (convertView==){
LayoutInflater inflater = getLayoutInflater()view = inflater.inflate(.)}{
view = convertView}
sivIcon = () view.findViewById(.)tvTitle = () view.findViewById(.)tvDetail = () view.findViewById(.)tvComment = () view.findViewById(.)newsInfo = .get(position)sivIcon.setImageUrl(newsInfo.getImageUrl())tvTitle.setText(newsInfo.getTitle())tvDetail.setText(newsInfo.getDetail())tvComment.setText(newsInfo.getComment()+)view}
(position) {
}
(position) {
}
}
(Menu menu) {
getMenuInflater().inflate(.menu)}
(MenuItem item) {
id = item.getItemId()(id == .) {
}
.onOptionsItemSelected(item)}
}
:网络访问时不可使用localhost或者127.0.0.1,否则会抛出访问被拒绝的异常错误
在android studio中导入jar包,直接将jar包考诶到libs目录下,右键将其添加进去即可
标签:
原文地址:http://my.oschina.net/zaizaiangels/blog/514037