描述:自定义listview控件,通过适配器来设置数据和图片,读取txt文件来获取景点介绍信息。
activity_main.xml文件代码:
xml version="1.0" encoding="utf-8"?>item.xml文件代码:xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#aabbcc" android:orientation="vertical" tools:context="scenicspot.com.scenicspot.MainActivity"> android:background="#ccbbaa" android:textColor="#000000" android:padding="10dp" android:gravity="center" android:text="@string/china_spot" android:textSize="24sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> android:id="@+id/scenery" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#aaaaaa" android:dividerHeight="2dp" android:gravity="center">
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/image" android:layout_width="100dp" android:layout_height="75dp" android:contentDescription="@string/imgText"/> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="vertical"> android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="20sp"/> android:id="@+id/brief" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="left" android:singleLine="true" android:ellipsize="end" android:textColor="#0000ee" android:textSize="12sp"/>
scenery_show.xml文件代码:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#aabbcc" android:orientation="vertical"> android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:background="#ccbbaa" android:textSize="24sp"/> android:id="@+id/image" android:layout_width="320dp" android:layout_height="240dp" android:scaleType="fitXY" android:contentDescription="@string/imgInfo"/> android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" android:textColor="#00ff00" android:textSize="16sp" android:padding="10dp"/>
MainActivity.java文件代码:
package scenicspot.com.scenicspot; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; public class MainActivity extends Activity { private ListView lv_scenery; private List
SceneryShowActivity.java文件代码:
package scenicspot.com.scenicspot; import android.app.Activity; import android.graphics.drawable.ClipDrawable; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Gravity; import android.widget.ImageView; import android.widget.TextView; import java.io.IOException; import java.io.InputStream; /** * Created by Administrator on 2017/4/19 0019. */ public class SceneryShowActivity extends Activity{ private ImageView imageView; private TextView content; private TextView title; private ClipDrawable clipDrawable; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.scenery_show); imageView = (ImageView) findViewById(R.id.image); content = (TextView) findViewById(R.id.content); title = (TextView) findViewById(R.id.title); String titleStr = getIntent().getStringExtra("name"); int image = getIntent().getIntExtra("image",R.drawable.wugong); int info = getIntent().getIntExtra("content",R.drawable.wugong1); title.setText(titleStr); clipDrawable = new ClipDrawable(getResources().getDrawable(image), Gravity.CENTER, ClipDrawable.HORIZONTAL); imageView.setImageDrawable(clipDrawable); startThread(); InputStream inputStream = getResources().openRawResource(info); content.setText(getStringFromInputStream(inputStream)); mHandler = new Handler() { @Override public void handleMessage(Message msg) { clipDrawable.setLevel(clipDrawable.getLevel() + 400); } }; } public String getStringFromInputStream(InputStream inputStream) { byte[] buffer = new byte[1024]; int hasRead = 0; StringBuilder result = null; result = new StringBuilder(""); try { while ((hasRead = inputStream.read(buffer)) != -1){ result.append(new String(buffer,0,hasRead,"GBK")); } } catch (Exception e) { e.printStackTrace(); } return result.toString(); } private void startThread() { clipDrawable.setLevel(0); new Thread(){ @Override public void run() { while (clipDrawable.getLevel() < 10000){ try { Thread.sleep(300); mHandler.sendEmptyMessage(0x11); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } }
raw文件截图:
运行效果图: