第一行代码 -3-1 软件也要拼脸蛋-UI界面



<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>


private String[] data={"a","b","c","e"};
@Override // 主界面初始构造函数
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(FirstActivity.this,"你点击了按钮1",Toast.LENGTH_SHORT).show();
ArrayAdapter<String> adapter= new ArrayAdapter<String>(FirstActivity.this,android.R.layout.simple_list_item_1,data);
ListView listview=(ListView)findViewById(R.id.list_view);
listview.setAdapter(adapter);
}
});
}




定制界面流程





package com.example.student.activitytest; /**
* Created by student on 2018/6/16.
*/ public class Fruit { private String name;
private int imageId; public Fruit(String name,int imageId){
this.imageId=imageId;
this.name=name;
} public String getName(){
return name; } public int getImageId(){
return imageId; } }



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView android:id="@+id/fruit_image"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/> <TextView android:id="@+id/fruit_name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/> </LinearLayout>



package com.example.student.activitytest; import android.content.Context;
import android.media.Image;
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 java.util.List; /**
* Created by student on 2018/6/16.
*/ public class FruitAdapter extends ArrayAdapter<Fruit>{ // 修改1:输入条目类型-自定义水果 名字 + 图片ID private int resourceId;// 水果定制布局 实参 fruit_item.xml
// 输入
// 参数1:总布局
// 参数2:水果定制布局 实参 fruit_item.xml
// 参数3:水果内容信息 名 + 图片Id
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){ //修改2:输入条目类型-自定义水果 名字 + 图片ID
super(context,textViewResourceId);
resourceId=textViewResourceId;// 水果定制布局 实参 fruit_item.xml } @Override
public View getView(int position, View convertView , ViewGroup parent){ Fruit fruit=getItem(position);// 修改3 -1 获当前是第几个水果 View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);// 3 -2 赋予水果定制布局 ImageView fruitImage=(ImageView)view.findViewById(R.id.fruit_image);// 修改3 -3分别将当前这个水果信息给对应控件
TextView fruitName=(TextView)view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view; } }







// 1-1水果内容
private List<Fruit> fruitList=new ArrayList<>();
// 1-2水果内容赋值
private void initFruits(){ for(int i=0;i<4;i++){ Fruit a=new Fruit("a",R.drawable.a);
fruitList.add(a);
Fruit b=new Fruit("a",R.drawable.c);
fruitList.add(b);
Fruit c=new Fruit("a",R.drawable.d);
fruitList.add(c);
} }
@Override // 主界面初始构造函数
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout); // 1-2水果内容赋值
initFruits();
// 2-1水果布局定制 水果定制适配器+ 水果内容=新的水果列表
FruitAdapter adapter= new FruitAdapter(FirstActivity.this,R.layout.fruit_item,fruitList);
// 3 创建显示列表,将适配后的水果显示
ListView listview=(ListView)findViewById(R.id.list_view);
listview.setAdapter(adapter); Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(FirstActivity.this,"你点击了按钮1",Toast.LENGTH_SHORT).show();}
});
}
第一行代码 -3-1 软件也要拼脸蛋-UI界面的更多相关文章
- 第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView
简述教程:https://www.jianshu.com/p/4fc6164e4709 一 基础准备 1 添加RecyclerView控件引用库文件 2 总布局添加RecyclerView控件 3 R ...
- 第一行代码 3-2-2 软件也要拼脸蛋-UI界面-更强大的滚动条- 卡片
<LinearLayout android:orientation="vertical" android:layout_width="match_parent&qu ...
- 第一行代码 3-5 软件也要拼脸蛋-UI界面-更强大的滚动条- 聊天室
动画 https://www.jianshu.com/p/4fc6164e4709 前面说过,RecyclerView可以设置列表中Item删除和添加的动画,在v7包中给我们提供了一种默认的Item删 ...
- 《第一行代码 android》 读书笔记:找出当前界面对应的Activity
在android开发中找出当前界面对应的Activity,步骤如下: 新建一个BaseActivity继承自Activity,然后在BaseActivity中重写onCreate()方法,通过getC ...
- 第三章:软件也要拼脸蛋-UI 开发的点点滴滴
常用控件 常用控件有:按钮 Button.文本显示框 TextView.图片显示框 ImageView.文本编辑框 EditText.进度条 ProgressBar.提示框 AlertDialog.进 ...
- 第一行代码 Android 第二版到货啦
今日android第一行代码[第二版]已到,收获的季节到了 先看一下封面 书签: 以后就把空闲时间送给它吧 先来看一下本书的目录: 第1章 开始启程--你的第1行Android代码 第2章 先从看得到 ...
- 通过《第一行代码》学习 Android 开发
第一行代码 Android --第 2 版-- 郭霖 著 第 1 章:开始启程--你的第一行 Android 代码 •1.2 手把手带你搭建开发环境 Android Studio 的安装及配置 A ...
- 第一行代码 Android (郭霖 著)
https://github.com/guolindev/booksource 第1章 开始启程----你的第一行Android代码 (已看) 第2章 先从看得到的入手----探究活动 (已看) 第3 ...
- HTML5 & CSS3初学者指南(1) – 编写第一行代码
介绍 网络时代已经到来.现在对人们来说,每天上网冲浪已经成为一种最为常见的行为. 在网页浏览器中输入一段文本地址,就像http://www.codeproject.com,等待一下,网页就加载到浏览器 ...
随机推荐
- Ajax事件,方法
1\ready:事件:使用ready()来使函数在文档加载后可用 $(document).ready(funcation(){ 函数体 }) 2\click事件:当单机元素时,使用 3\focus事件 ...
- 近期ASP.NET问题汇总及对应的解决办法
1. 使用SQL统计一个字符串中指定字符的个数,示例(统计0的个数): ','')) 2. 使用Forms认证,客户端本地时间不对无法登陆系统,解决办法: FormsAuthentication.Re ...
- HDU6213
Chinese Zodiac Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- Android实现两次点击返回键提示退出
Android的很多app中,都有点击一次返回键提示再次点击退出app的功能. 今天就看了下实现的方式,其实就是在相应的Activity中重写了onKeyDown()方法.在onKeyDown()方法 ...
- 微信小程序踩过的一些坑
前言 迄今为止,正儿八经的上线了真正意义上的程序,但是这个小程序却着实不小. 之所以不小,是因为这个类似于社区的小程序,已经做了大部分都有的功能了 举例说明,具体的一些功能点: 1.帖子列表页面:会有 ...
- [iOS]深入理解GCD
看到一篇很好的文章,本来想翻译的,但发现已经有人翻译了,遂简单整理了一下,方便阅读学习 新博客[wossoneri.com] 什么是GCD GCD(Grand Central Dispatch)是li ...
- (网页)alert()怎么回事出不来,代码没有写错
1.不报错,请查看浏览器是否禁掉了alter. 2.console.log()输出,避免这种尴尬.
- OCaml相关
1.open Core.Std 时报Unbound module Core 先安装库 $ opam init $ opam install core $ opam install utop 在~/.o ...
- bs4爬虫入门
# -*- coding: utf-8 -*- """ Created on Fri Nov 16 13:35:33 2018 @author: zhen "& ...
- python redis 终端 redis-cli.py mini版本 redis 终端管理工具
Python redis-cli.py Python3 redis-cli 命令行管理工具 redis终端工具 由于最近测试redis未授权访问漏洞,发现本机没有安装redis,不能运行redis-c ...