|
目录结构
第一步
value/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">你选择了</string>
<string name="app_name">高级控件</string>
<string name="andy">Andy Rubin \nAndroid的创造者</string>
<string name="bill">Bill Joy \njava的创造者</string>
<string name="edgar">Edgar F. Codd \n关系数据库之父</string>
<string name="torvalds">Linus Torvalds \nLinux之父</string>
<string name="turing">Turing Alan \nIT的祖师爷</string>
<string name="ys">你选择了</string>
</resources>
复制代码
第二步
value/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#fd8d8d</color>
<color name="green">#9cfda3</color>
<color name="blue">#8d9dfd</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="gray">#050505</color>
</resources>
复制代码
第三步
layout/list_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dip"
android:textColor="@color/white"
android:text="@string/hello" />
<ListView android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
复制代码
第四步
ListViewActivity.java
package com.taofu5;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
public class ListViewActivity extends Activity {
int[] msgIds = {R.string.andy,R.string.bill,
R.string.edgar, R.string.torvalds,R.string.turing};
int[] imgIds = {R.drawable.andy, R.drawable.bill,
R.drawable.edgar, R.drawable.torvalds, R.drawable.turing};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_layout);
ListView listView = (ListView) this.findViewById(R.id.listview);
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return imgIds.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int num, View view, ViewGroup group) {
LinearLayout ll = new LinearLayout(ListViewActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(5, 5, 5, 5);
ImageView imageView = new ImageView(ListViewActivity.this);
imageView.setImageDrawable(getResources().getDrawable(imgIds[num]));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 98));
ll.addView(imageView);
TextView textView = new TextView(ListViewActivity.this);
textView.setText(getResources().getText(msgIds[num]));
textView.setTextSize(24);
textView.setTextColor(
ListViewActivity.this.getResources().getColor(R.color.blue));
textView.setPadding(5, 5, 5, 5);
textView.setGravity(Gravity.LEFT);
ll.addView(textView);
return ll;
}
};
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View view,
int num, long lnum) {
TextView textView = (TextView) findViewById(R.id.textview);
LinearLayout layout = (LinearLayout) view;
TextView subTextView = (TextView)layout.getChildAt(1);
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(getResources().getText(R.string.ys));
strBuilder.append(":" + subTextView.getText());
String stemp = strBuilder.toString();
textView.setText(stemp.split("\\n")[0]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int num,
long lnum) {
TextView textView = (TextView) findViewById(R.id.textview);
LinearLayout layout = (LinearLayout) view;
TextView subTextView = (TextView)layout.getChildAt(1);
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(getResources().getText(R.string.ys));
strBuilder.append(":" + subTextView.getText());
String stemp = strBuilder.toString();
textView.setText(stemp.split("\\n")[0]);
}
});
}
}
复制代码
第五步:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.taofu5"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ListViewActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
复制代码
效果截图如下
|
|