1.在res/value下建立mytitlebar.xml文件<?xml version="1.0" encoding="utf-8"?><resources xmlns:android=" <style name="myTitleBg"> <item name="android:background">#FF</item> </style> <style name="myTheme" parent="android:Theme"> <item name="android:windowNoTitle">false</item> <item name="android:windowTitleSize">dp</item> <item name="android:windowTitleBackgroundStyle">@style/myTitleBg</item> </style></resources>2.修改AndroidManifest.xml文件 android:theme="@style/myTheme"代码为:MainActivity.javapackage com.example.ui_listview;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import com.example.ui_listview.adpater.FruitAdapter;import com.example.ui_listview.model.Fruit;public class MainActivity extends Activity {private ListView lvFruits;//1.准备好数据源private List<Fruit> fruitList = new ArrayList<Fruit>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//1.准备好数据源initFruits();lvFruits = (ListView) findViewById(R.id.lvFruits);//2.创建Adapter FruitAdapter adapter=new FruitAdapter(this, R.layout.fruit_item,fruitList); //3.绑定AdapterView和Adapter之间的关联lvFruits.setAdapter(adapter);lvFruits.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) { Fruit fruit=fruitList.get(position); Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();}});}private void initFruits() {Fruit apple = new Fruit("王乐", R.drawable.p1,"今天打游戏了吗?","1分钟前");fruitList.add(apple);Fruit banana = new Fruit("李旺", R.drawable.p2,"我也不知道啊","5分钟前");fruitList.add(banana);Fruit orange = new Fruit("剑姬", R.drawable.p3,"撸一把","分钟前");fruitList.add(orange);Fruit watermelon = new Fruit("撒旦", R.drawable.p4,".......","1小时前");fruitList.add(watermelon);Fruit pear = new Fruit("大嫂", R.drawable.p5,"发说说了吗","1.5小时前");fruitList.add(pear);Fruit grape = new Fruit("小明", R.drawable.p6,"发了!","2小时前");fruitList.add(grape);Fruit pineapple = new Fruit(" 奥迪", R.drawable.p7,"嗷嗷嗷叫","6小时前");fruitList.add(pineapple);Fruit strawberry = new Fruit("上单", R.drawable.p8,"必须","1天前");fruitList.add(strawberry);Fruit cherry = new Fruit("中单", R.drawable.p,"ok","1周前");fruitList.add(cherry);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}FruitAdapter.javapackage com.example.ui_listview.adpater;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.ui_listview.R;import com.example.ui_listview.model.Fruit;//存在问题:在FruitAdapter的getView()方法中每次都将布局重新加载了一遍,当ListView快速滚动的时候这就会成为性能的瓶颈public class FruitAdapter extends ArrayAdapter<Fruit> {private int resourceId;public FruitAdapter(Context context, int textViewResourceId,List<Fruit> data) {super(context, textViewResourceId, data);resourceId=textViewResourceId;}// 这个方法在每个子项被滚动到屏幕内的时候会被调用@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Fruit fruit = getItem(position);View view=LayoutInflater.from(getContext()).inflate(resourceId, null);ImageView ivFruit=(ImageView) view.findViewById(R.id.ivFruit);TextView tvFruit=(TextView) view.findViewById(R.id.tvFruit);TextView textView=(TextView)view.findViewById(R.id.textView);TextView textView2=(TextView)view.findViewById(R.id.textView2);ivFruit.setImageResource(fruit.getImageId());tvFruit.setText(fruit.getName());textView.setText(fruit.getText());textView2.setText(fruit.getTime());return view;}}FruitAdapterImprove1.javapackage com.example.ui_listview.adpater;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.ui_listview.R;import com.example.ui_listview.model.Fruit;//存在问题:虽然现在已经不会再重复去加载布局,但是每次在getView()方法中还是会调用View的findViewById()方法来获取一次控件的实例。public class FruitAdapterImprove1 extends ArrayAdapter {private int resourceId;public FruitAdapterImprove1(Context context, int textViewResourceId) {super(context, textViewResourceId);resourceId = textViewResourceId;}// 这个方法在每个子项被滚动到屏幕内的时候会被调用//getView()方法中还有一个convertView参数,这个参数用于将之前加载好的布局进行缓存,以便之后可以进行重用@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Fruit fruit = (Fruit) getItem(position);View view=null;if(convertView==null) view = LayoutInflater.from(getContext()).inflate(resourceId, null);else view=convertView;ImageView fruitImage = (ImageView) view.findViewById(R.id.ivFruit);TextView fruitName = (TextView) view.findViewById(R.id.tvFruit);fruitImage.setImageResource(fruit.getImageId());fruitName.setText(fruit.getName());return view;}}FruitAdapterImprove2.javapackage com.example.ui_listview.adpater;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.ui_listview.R;import com.example.ui_listview.model.Fruit;//我们新增了一个内部类ViewHolder,用于对控件的实例进行缓存。//当convertView为空的时候,创建一个ViewHolder对象,并将控件的实例都存放在ViewHolder里,然后调用View的setTag()方法,将ViewHolder对象存储在View中。//当convertView不为空的时候则调用View的getTag()方法,把ViewHolder重新取出。//这样所有控件的实例都缓存在了ViewHolder里,就没有必要每次都通过findViewById()方法来获取控件实例了。public class FruitAdapterImprove2 extends ArrayAdapter {private int resourceId;public FruitAdapterImprove2(Context context, int textViewResourceId) {super(context, textViewResourceId);resourceId = textViewResourceId;}// 这个方法在每个子项被滚动到屏幕内的时候会被调用// getView()方法中还有一个convertView参数,这个参数用于将之前加载好的布局进行缓存,以便之后可以进行重用@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Fruit fruit = (Fruit) getItem(position);View view = null;ViewHolder viewHolder;if (convertView == null) {view = LayoutInflater.from(getContext()).inflate(resourceId, null);viewHolder = new ViewHolder();viewHolder.ivFruit = (ImageView) view.findViewById(R.id.ivFruit);viewHolder.tvFruit = (TextView) view.findViewById(R.id.tvFruit);view.setTag(viewHolder);} else {view = convertView;viewHolder = (ViewHolder) convertView.getTag();}viewHolder.ivFruit.setImageResource(fruit.getImageId());viewHolder.tvFruit.setText(fruit.getName());return view;}class ViewHolder {ImageView ivFruit;TextView tvFruit;}}Fruit.javapackage com.example.ui_listview.model;public class Fruit {private String name;private int imageId;private String text;private String time;public Fruit(String name, int imageId, String text, String time) {super();this.name = name;this.imageId = imageId;this.text = text;this.time = time;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getImageId() {return imageId;}public void setImageId(int imageId) {this.imageId = imageId;}public String getText() {return text;}public void setText(String text) {this.text = text;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}}fruit_item.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@id/ivFruit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/p1" /> <TextView android:id="@id/tvFruit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="dp" android:layout_marginLeft="dp" android:layout_toRightOf="@id/ivFruit" android:text="Apple" /> <TextView android:id="@id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/tvFruit" android:layout_below="@id/tvFruit" android:layout_marginLeft="dp" android:text="今天真高兴" /> <TextView android:id="@id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@id/tvFruit" android:layout_alignBottom="@id/tvFruit" android:layout_alignParentRight="true" android:layout_marginRight="dp" android:text="TextView" /></RelativeLayout>activity_main.xml<LinearLayout xmlns:android=" xmlns:tools=" android:id="@id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ListView android:id="@id/lvFruits" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android=" package="com.example.ui_listview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/myTheme" > <activity android:name="com.example.ui_listview.MainActivity" 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></manifest>}
推荐整理分享安卓-新浪微博评论(新浪微博 app 安卓),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:emui新浪微博,安卓新浪微博怎么夜间模式设置,安卓新浪微博怎么设置关注仅自己可见,安卓新浪微博怎么夜间模式设置,新浪微博 安卓,新浪微博 安卓,安卓新浪微博怎么设置关注仅自己可见,安卓新浪微博怎么夜间模式设置,内容如对您有帮助,希望把文章链接给更多的朋友!
安卓域名转IP地址(获取Response Header的Location) packagecom.itant.ipconvert;importorg.apache.http.Header;importorg.apache.http.HttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;importorg.ap
GridView编写的一个图片浏览器 学完GridView,做了一个图片浏览的功能的小组件。什么是GridView呢?GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,
菜鸟的安卓实习之路---layoutParams 在开发项目的时候,需要用一个第三方的滑动switch(slideSwitch)控件,但是在xml文件当中设置参数,比如,宽,高,上下左右margin,居中等,都失效了,然后