欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

安卓开发中的几种常见视图

时间:2023-07-05
列表视图

常用属性Adapter接口

package com.example.zzm.helloworld.listview;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.Toast;import com.example.zzm.helloworld.R;public class ListViewActivity extends Activity { private ListView mLv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); mLv1=(ListView)findViewById(R.id.lv_1); mLv1.setAdapter(new MyListAdapter(ListViewActivity.this)); //点击事件 mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { Toast.makeText(ListViewActivity.this,"点击pos:"+position, Toast.LENGTH_SHORT).show(); } }); mLv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) { Toast.makeText(ListViewActivity.this,"长按 pos:"+position, Toast.LENGTH_SHORT).show(); return true; } }); }}`````xml<?xml version="1.0" encoding="utf-8"?>

package com.example.zzm.helloworld.listview;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.baseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.zzm.helloworld.R;import org.w3c.dom.Text;public class MyListAdapter extends baseAdapter{ private Context mContext; private LayoutInflater mLayoutInflater; public MyListAdapter(Context context){ this.mContext=context; mLayoutInflater=LayoutInflater.from(context); } @Override public int getCount() { return 10; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } public class ViewHolder{ public ImageView imageView; public TextView tvTitle,tvTime,tvContent; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; if(convertView==null){ convertView=mLayoutInflater.inflate(R.layout.layout_list_view,null); holder=new ViewHolder(); holder.imageView=(ImageView)convertView.findViewById(R.id.iv); holder.tvTitle=(TextView)convertView.findViewById(R.id.tv_title); holder.tvTime=(TextView)convertView.findViewById(R.id.tv_time); holder.tvContent=(TextView)convertView.findViewById(R.id.tv_content); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } //给控件赋值 holder.tvTitle.setText("这是标题"); holder.tvTime.setText("2088-08-08"); holder.tvContent.setText("这是内容"); Glide.with(mContext).load("").into(holder.imageView); return convertView; return null; }}

<?xml version="1.0" encoding="utf-8"?>

网格视图

<?xml version="1.0" encoding="utf-8"?>

<?xml version="1.0" encoding="utf-8"?>

ScrollView视图

垂直滚动:ScrollView:
子元素只能有一个

<?xml version="1.0" encoding="utf-8"?>

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。