直接继承View.较简单
设置监听器方式
ImageButton按钮中添加的图片上有灰底的,可以通过background="#0000"来去除
9-Patch图片前提是必须存储为PNG-24
{
OnDateChangedListener
}
{
OnTimeChangedListener
setIs24HourView(true) 改为24制式
}
实现分5步:
//获取我们关心的组件
ch = (Chronometer)findViewById(R.id.test);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ch.setBase(SystemClock.elapsedRealtime());//设置计时时间
ch.start();
start.setEnabled(false);
}
});
//邦定时间监听20秒
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if(SystemClock.elapsedRealtime()-ch.getBase()>20*1000){
ch.stop();
start.setEnabled(true);
}
}
});
A.为ImageSwitcher提供一个ViewFactory,该ViewFactory生成的View必须是
ImageView
B:需要切换图片时,只要调用ImageSwitcher的
切换动画的话:
is.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.slide_in_left)
setOutAnimationAnimationUtils.loadAnimation(MainActivity.this,R.anim.slide_out_right)
关键代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = findViewById(R.id.gridView);
//设置simpleAdapter中的参数
List
//创建一个类,继承BaseAdapter
//定义ImageAdapter类
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context mContext) {
this.mContext = mContext;
}
@Override
public int getCount() {
return picture.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
//判断传过来的convertView是否为空,是的话创建
if(convertView==null){
imageView = new ImageView(mContext);
//设置新生成的ImageView的长宽
imageView.setLayoutParams(new GridView.LayoutParams(1400,1800));
//设置缩放(保持纵横比缩放
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}else{ //如果传递过来的imageView不为空,把传过来的给imageView
imageView =(ImageView)convertView;
}
imageView.setImageResource(picture[position]);
return imageView;
}
}
第一种方式 entries:设置列表框内容
第二种方式 利用适配器ArrayAdapter
public class MainActivity extends AppCompatActivity {
private int[] images ={
R.mipmap.q3,R.mipmap.q1,R.mipmap.q2,
R.mipmap.q1,R.mipmap.q3,R.mipmap.q1,
R.mipmap.q2,R.mipmap.q3
};
private String[] title =new String[]{
"刘一","张三","李四","王麻子","李成","大三","唐伯虎","罗四"
};
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.listview);
List> listItem = new ArrayList>();
//定义一个list分别把图标和名称放入
for(int i=0;i map = new HashMap();
map.put("image",images[i] );
map.put("name",title[i]);
listItem.add(map);
}
SimpleAdapter simpleAdapter =new SimpleAdapter(this,listItem,R.layout.main,new String[]{"image","name"},
new int [] {R.id.image,R.id.title});
lv.setAdapter(simpleAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Map map = (Map) parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this,
map.get("name").toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
1.添加依赖库
dependencies { ... implementation 'com.android.support:recyclerview-v7:28.0.0' }
2.编写主界面
?xml version="1.0" encoding="utf-8"?>
3.编写消息类Msg
//定义一个消息的实体类
public class Msg {
//表示这是一条收到的消息
public static final int TYPE_RECEIVED =0;
//表示这是一条发送的消息
public static final int TYPE_SENT=1;
private String content; //消息内容
private int type; //消息类型
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public String getContent() {
return content;
}
public int getType() {
return type;
}
}
4.编写RecyclerView的子项布局
5.RecyclerView的适配器类 MsgAdapter
public class MsgAdapter extends RecyclerView.Adapter {
private List mMsgList;
static class ViewHoler extends RecyclerView.ViewHolder{
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
public ViewHoler(View view) {
super(view);
leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
leftMsg = (TextView)view.findViewById(R.id.left_msg);
rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
rightMsg = (TextView) view.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List msgList){
mMsgList=msgList;
}
@Override
public ViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate
(R.layout.msg_item,parent,false);
return new ViewHoler(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHoler holder, int position) {
Msg msg = mMsgList.get(position);
if(msg.getType()==msg.TYPE_RECEIVED){
//如果是收到的消息,,则显示左边的消息布局,将右边的布局隐藏
holder.leftLayout.setVisibility(View.VISIBLE);
holder.rightLayout.setVisibility(View.GONE);
holder.leftMsg.setText(msg.getContent());
}else if(msg.getType()==Msg.TYPE_SENT){
//如果是发出的消息,则显示右边的消息布局,将左边的消息布局隐藏
holder.rightLayout.setVisibility(View.VISIBLE);
holder.leftLayout.setVisibility(View.GONE);
holder.rightMsg.setText(msg.getContent());
}
}
@Override
public int getItemCount() {
return mMsgList.size();
}
}
6.主程序中:
public class MainActivity extends AppCompatActivity {
private List msgList =new ArrayList<>();
private EditText inputText;
private Button send;
private RecyclerView msgRecyclerView;
private MsgAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化消息数据
initMsgs();
send =findViewById(R.id.send);
inputText= findViewById(R.id.input_text);
msgRecyclerView = findViewById(R.id.msg_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
msgRecyclerView.setLayoutManager(layoutManager);
adapter=new MsgAdapter(msgList);
msgRecyclerView.setAdapter(adapter);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = inputText.getText().toString();
if(!"".equals(content)){
Msg msg=new Msg(content,Msg.TYPE_SENT);
msgList.add(msg);
//当有新消息时,刷新ListView中的显示
adapter.notifyItemInserted(msgList.size()-1);
//将ListView定位到最后一行
msgRecyclerView.scrollToPosition(msgList.size()-1);
inputText.setText("");
}
}
});
}
private void initMsgs(){
Msg msg1 = new Msg("Hello guy.",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2 = new Msg("Hello,Who is that?",Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3 = new Msg("This is Tim,Nice talking to you.",Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
}
TabHost的Id 必须是android预定义的ID @android:
关键代码
代码文件
TabHost tabHost = findViewById(android.R.id.tabhost);
//tabhost需要初始化
tabHost.setup();
//初始化完成后添加标签页
LayoutInflater layoutInflater = LayoutInflater.from(this);
layoutInflater.inflate(R.layout.tab1,tabHost.getTabContentView());
layoutInflater.inflate(R.layout.tab2,tabHost.getTabContentView());
//添加标签页
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("精选表情").setContent(R.id.left));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("投稿表情").setContent(R.id.right));