admin管理员组文章数量:1794759
安卓
文章目录
- 1. ListView实现通讯录
- 实现效果
- xml代码
- activity_tongxunlu
- item_pyq
- Java代码
- MyBean
- MyAdapter
- MyActivityTongxunlu
- 2. ListView微信朋友圈:文本、图片、超链接
- 实现效果
- xml代码
- activity_pyq
- item_pyq
- Java代码
- MyBean
- MyAdapter
- Myactivity
- ImageView设置图片自适应的方法
1. ListView实现通讯录
实现效果
xml代码
activity_tongxunlu
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/top"android:layout_width="match_parent"android:layout_height="80dp"android:background="#FFEEEEEE"android:orientation="horizontal"><TextViewandroid:id="@+id/tongxunlu"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_marginTop="20dp"android:layout_marginLeft="145dp"android:text="通讯录"android:gravity="center"android:textSize="30sp"android:textColor="@color/black"/></LinearLayout><LinearLayoutandroid:id="@+id/bott"android:layout_width="match_parent"android:layout_height="80dp"android:layout_alignParentBottom="true"android:background="#FFEEEEEE"android:orientation="horizontal"></LinearLayout><ListViewandroid:id="@+id/list_view"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/bott"android:layout_below="@+id/top"></ListView></RelativeLayout>
item_pyq
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app=""><ImageViewandroid:id="@+id/icon"android:layout_width="90dp"android:layout_height="50dp"android:layout_marginTop="10dp"android:layout_marginLeft="16dp"android:src="@mipmap/zh_4"android:scaleType="fitStart"/><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/icon"android:layout_marginTop="20dp"android:text="我是姓名"android:textSize="23sp"android:textStyle="bold"android:gravity="center_vertical"/>
</RelativeLayout>
Java代码
MyBean
public class MyBean {private int icon; //用户的头像private String name; //用户名public MyBean(){}public MyBean(int icon, String name) {this.icon = icon;this.name = name;}public int getIcon() {return icon;}public String getName() {return name;}public void setIcon(int icon) {this.icon = icon;}public void setName(String name) {this.name = name;}
}
MyAdapter
public class MyAdapter extends BaseAdapter {private List<MyBean> mList;private LayoutInflater inflater;private Context context;public MyAdapter(List<MyBean> mList, Context context) {this.mList = mList;this.context = context;inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return mList == null ? 0 : mList.size();}@Overridepublic Object getItem(int i) {return mList.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {ViewHolder viewHolder = null;if(view == null) {viewHolder = new ViewHolder();view = inflater.inflate(R.layout.item_tongxunlu,null);viewHolder.imageView = view.findViewById(R.id.icon);viewHolder.name = view.findViewById(R.id.name);view.setTag(viewHolder);} else {viewHolder = (ViewHolder) view.getTag();}MyBean myBean = mList.get(i);viewHolder.imageView.setImageResource(myBean.getIcon());viewHolder.name.setText(myBean.getName());return view;}public class ViewHolder {public ImageView imageView;private TextView name;}
}
MyActivityTongxunlu
public class MyActivityTongxunlu extends Activity implements AdapterView.OnItemClickListener{private ListView listView;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tongxunlu);InitView();initMyadapter();}private void InitView() {listView = findViewById(R.id.list_view);}private void initMyadapter() {MyAdapter myAdapter = new MyAdapter(getData(), this);listView.setAdapter(myAdapter);}private List<MyBean> getData() {List<MyBean> mList = new ArrayList<>();MyBean myBean = new MyBean();myBean.setIcon(R.mipmap.zh_4);myBean.setName("安琪拉");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_8);myBean.setName("扁鹊");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_9);myBean.setName("不知火舞");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_12);myBean.setName("嫦娥");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_13);myBean.setName("沈梦溪");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_17);myBean.setName("妲己");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_21);myBean.setName("貂蝉");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_25);myBean.setName("干将莫邪");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_26);myBean.setName("高渐离");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_32);myBean.setName("海月");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_38);myBean.setName("姜子牙");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_39);myBean.setName("金蝉");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_63);myBean.setName("米莱狄");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_66);myBean.setName("墨子");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_70);myBean.setName("女娲");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_74);myBean.setName("上官婉儿");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_76);myBean.setName("司马懿");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_83);myBean.setName("王昭君");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_84);myBean.setName("武则天");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_85);myBean.setName("西施");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_89);myBean.setName("小乔");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_93);myBean.setName("杨玉环");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_96);myBean.setName("奕星");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_97);myBean.setName("嬴政");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_103);myBean.setName("张良");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_105);myBean.setName("甄姬");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_108);myBean.setName("周瑜");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_109);myBean.setName("诸葛亮");mList.add(myBean);return mList;}private void setListView() {listView.setOnItemClickListener(this);}@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {if(i == 0) {Log.e("cc","点击了,第"+i+"项");} else {Intent intent = new Intent(this, MainActivity.class);startActivity(intent);}}
}
2. ListView微信朋友圈:文本、图片、超链接
实现效果
xml代码
activity_pyq
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"><FrameLayoutandroid:id="@+id/top"android:layout_width="match_parent"android:layout_height="280dp"android:background="#FFEEEEEE"android:orientation="horizontal"><ImageViewandroid:id="@+id/bg"android:layout_width="match_parent"android:layout_height="230dp"android:gravity="center"android:src="@mipmap/bg_0"android:scaleType="centerCrop"/><ImageViewandroid:id="@+id/tx"android:layout_width="80dp"android:layout_height="80dp"android:layout_marginLeft="320dp"android:layout_marginTop="190dp"android:src="@mipmap/fu_10"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="200dp"android:layout_marginLeft="243dp"android:text="蔡文姬"android:textSize="25sp"android:textColor="#010101"android:textStyle="bold"/></FrameLayout><ListViewandroid:id="@+id/list_view"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@+id/top"></ListView></RelativeLayout>
item_pyq
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#EEEEEE"><!-- 好友头像--><ImageViewandroid:id="@+id/user_icon"android:layout_width="70dp"android:layout_height="70dp"android:layout_marginLeft="16dp" /><TextViewandroid:id="@+id/user_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/user_icon"android:layout_marginLeft="95dp"android:layout_marginRight="32dp"android:ellipsize="end"android:singleLine="true"android:text="我是姓名"android:textColor="#002AFF"android:textSize="21sp" /><TextViewandroid:id="@+id/user_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="95dp"android:layout_marginTop="43dp"android:layout_marginRight="10dp"android:autoLink="all"android:textColor="#000000"android:textSize="21sp" /><ImageViewandroid:id="@+id/text_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/user_text"android:layout_marginLeft="95dp"android:adjustViewBounds="true"android:maxWidth="220dp"android:maxHeight="220dp"android:scaleType="centerCrop"/><ImageViewandroid:id="@+id/text_icon2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/user_name"android:layout_marginTop="5dp"android:layout_marginLeft="95dp"android:adjustViewBounds="true"android:maxWidth="130dp"android:maxHeight="130dp"/><ImageViewandroid:id="@+id/text_icon3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/user_name"android:layout_marginTop="5dp"android:layout_marginLeft="230dp"android:adjustViewBounds="true"android:maxWidth="130dp"android:maxHeight="130dp"/>
</RelativeLayout>
Java代码
MyBean
public class MyBean {private int icon, texticon;private String name, usertext;private int texticon2, texticon3;public MyBean() {}public int getTexticon2() {return texticon2;}public void setTexticon2(int texticon2) {this.texticon2 = texticon2;}public int getTexticon3() {return texticon3;}public void setTexticon3(int texticon3) {this.texticon3 = texticon3;}public MyBean(int icon, int texticon, String name, String usertext, int texticon2, int texticon3) {this.icon = icon;this.texticon = texticon;this.name = name;this.usertext = usertext;this.texticon2 = texticon2;this.texticon3 = texticon3;}public int getTexticon() {return texticon;}public void setTexticon(int texticon) {this.texticon = texticon;}public int getIcon() {return icon;}public void setIcon(int icon) {this.icon = icon;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getUsertext() {return usertext;}public void setUsertext(String usertext) {this.usertext = usertext;}
}
MyAdapter
public class MyAdapter extends BaseAdapter {private List<MyBean> mList;private LayoutInflater inflater;public MyAdapter(List<MyBean> mList, Context context) {this.mList = mList;inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return mList == null ? 0 : mList.size();}@Overridepublic Object getItem(int i) {return mList.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {ViewHolder viewHolder = null;if(view == null) {viewHolder = new ViewHolder();view = inflater.inflate(R.layout.item_pyq, null);viewHolder.imageView = view.findViewById(R.id.user_icon);viewHolder.name = view.findViewById(R.id.user_name);viewHolder.usertext = view.findViewById(R.id.user_text);viewHolder.imageView2 = view.findViewById(R.id.text_icon);viewHolder.imageView3 = view.findViewById(R.id.text_icon2);viewHolder.imageView4 = view.findViewById(R.id.text_icon3);view.setTag(viewHolder);} else {viewHolder = (ViewHolder) view.getTag();}//获取数据MyBean myBean = mList.get(i);viewHolder.imageView.setImageResource(myBean.getIcon());viewHolder.name.setText(myBean.getName());viewHolder.usertext.setText(myBean.getUsertext());viewHolder.imageView2.setImageResource(myBean.getTexticon());viewHolder.imageView3.setImageResource(myBean.getTexticon2());viewHolder.imageView4.setImageResource(myBean.getTexticon3());return view;}public class ViewHolder {public ImageView imageView;private TextView name;private TextView usertext;public ImageView imageView2;public ImageView imageView3;public ImageView imageView4;}
}
Myactivity
public class MyActivity extends Activity implements AdapterView.OnItemClickListener{private ListView listView;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_pyq);InitView();initMyadapter();setListView();}private void InitView(){listView = findViewById(R.id.list_view);}private void initMyadapter() {MyAdapter myAdapter = new MyAdapter(getData(), this);listView.setAdapter(myAdapter);}private List<MyBean> getData() {List<MyBean> mList = new ArrayList<>();MyBean myBean = new MyBean();myBean.setIcon(R.mipmap.zh_21);myBean.setName("貂蝉");myBean.setUsertext("要画出一幅最美好、最快乐的场景,这是那个画匠的使命。\n" +"他不禁想起了多年前的那个上元夜,来自遥远西域的舞姬,在长安的盛大灯火之中,所跳的那一舞胡旋...\n" +"点击链接,了解更多的皮肤故事:"+"");mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_17);myBean.setName("妲己");myBean.setTexticon(R.mipmap.bg_0);mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_109);myBean.setName("诸葛亮");myBean.setUsertext("当手持双枪的青年,与身披枷锁的深海之王相遇\n" +"他,又会做出怎样的选择\n" +"今天,诸小亮将为大家带来马可波罗深海之息皮肤海报~\n");myBean.setTexticon(R.mipmap.bg_2);mList.add(myBean);myBean = new MyBean();myBean.setIcon(R.mipmap.zh_83);myBean.setName("王昭君");myBean.setTexticon2(R.mipmap.bg_3);myBean.setTexticon3(R.mipmap.bg_1);mList.add(myBean);return mList;}private void setListView() {listView.setOnItemClickListener(this);}@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {Log.e("cc", "点击了,第" + i + "项");}
}
ImageView设置图片自适应的方法
参考链接:
adjustViewBounds:调整ImageView的边界,使得ImageView和图片有一样的长宽比例
常用语句:
android:adjustViewBounds="true"android:maxWidth="210dp"android:maxHeight="210dp"
本文标签: 安卓
版权声明:本文标题:安卓 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1692797087a205181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论