(一)Android常用控件及简单用法

  1、如下图:

  2、补充:

  (1)margin:外边距;padding:内边距。

  (2)gravity:子元素的位置;layout_gravity:子元素在父元素中的位置。

  (3)当布局方向为横向时,不能指定子元素在横向上的对齐方式;竖向同理。

  (二)四种布局

  1、布局与控件的嵌套关系:

  2、四种基本布局

  (三)自定义控件的使用  

  1、Android中控件和布局的继承结构图:

  2、在xml文件中引入布局

  假如新建了一个名为title.xml的布局文件,作为标题栏,然后在activity_main.xml中可以用<include layout="@layout/title" />这样的方法引入title.xml的布局。

  3、创建自定义控件并为控件中的元素添加点击事件:

  (1)title_base.xml和color.xml(用于保存常用颜色)

  title_base.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/TitleBaseBg"
android:orientation="horizontal" > <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:orientation="vertical" > <ImageButton
android:id="@+id/title_base_left_ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/Transparent"
android:padding="5dp"
android:src="@drawable/back1_64" />
</LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" > <TextView
android:id="@+id/title_base_middle_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的App"
android:textColor="@drawable/White"
android:textSize="20sp" />
</LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="right"
android:orientation="vertical" > <ImageButton
android:id="@+id/title_base_right_ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/Transparent"
android:padding="5dp"
android:src="@drawable/add4_64" />
</LinearLayout> </LinearLayout>

  color.xml:

 <?xml version="1.0" encoding="utf-8"?>
<resources> <drawable name="TitleBaseBg">#ff272636</drawable>
<drawable name="Transparent">#00ffffff</drawable>
<drawable name="White">#ffffffff</drawable> </resources>

  (2)BaseTitleLayout.java,是个抽象类,继承自LinearLayout:

 public abstract class BaseTitleLayout extends LinearLayout {
protected ImageButton titleBaseLeftIb;
protected TextView titleBaseMiddleTv;
protected ImageButton titleBaseRightIb; public BaseTitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_base, this); titleBaseLeftIb = (ImageButton) findViewById(R.id.title_base_left_ib);
titleBaseMiddleTv = (TextView) findViewById(R.id.title_base_middle_tv);
titleBaseRightIb = (ImageButton) findViewById(R.id.title_base_right_ib); changeUI();
onLeftClick();
onRightClick();
} // 改变标题栏按钮、文字、背景等
protected abstract void changeUI(); // 标题栏左边按钮的点击事件
protected abstract void onLeftClick(); // 标题栏右边按钮的点击事件
protected abstract void onRightClick(); }

  (3)MainActivityTitleLayout.java,继承自BaseTitleLayout:

 public class MainActivityTitleLayout extends BaseTitleLayout {

     public MainActivityTitleLayout(Context context, AttributeSet attrs) {
super(context, attrs); } @Override
protected void changeUI() {
titleBaseLeftIb.setVisibility(View.INVISIBLE);
} @Override
protected void onLeftClick() { } @Override
protected void onRightClick() {
titleBaseRightIb.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(getContext(), "点击了添加按钮", Toast.LENGTH_SHORT)
.show();
}
});
} }

  (4)activity_main.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <com.easydo.layout.MainActivityTitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.easydo.layout.MainActivityTitleLayout> </LinearLayout>

  (5)MainActivity:

 public class MainActivity extends Activity {

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}

  (6)运行效果:

 

  (四)ListView的用法

  1、最简单的用法

  (1)xml文件:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ListView
android:id="@+id/content_lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>

  (2)MainActivity(详细步骤见注释)

 public class MainActivity extends Activity {
// 1.创建数据数组
private String[] animalList = { "猫", "狗", "狐狸", "小熊", "鱼", "老虎",
"长颈鹿", "象", "龙猫" }; ListView listView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 2.创建以数据列表元素类型为泛型的适配器
// 构造函数:第一个参数为上下文;第二个参数为列表项的布局,这里用Android自带的布局;第三个参数为第1步中准备好的数据数组.
// simple_list_item_1:单行显示,其中只有一个TextView
// simple_list_item_2:双行显示,有两个TextView,两行字大小不一样
// two_line_list_item:双行显示,有两个TextView,两行字大小一样
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,
animalList);
// 3.获取xml中的ListView实例
listView = (ListView) findViewById(R.id.content_lv); // 4.用第2步创建好的适配器来设置ListView实例的内容
listView.setAdapter(adapter);
}
}

  (3)运行结果:

  2、定制的ListView界面

  上面的ListView每个项只能显示一个文本,太单调了,下面通过定制的方式让它丰富起来。实现左边显示一个图片,右边显示动物名字的效果。

  步骤如下:

  (1)创建一个实体类Animal,作为ListView适配器的类型:

 public class Animal {
private String name;
private int imageId; public Animal(String name, int imageId) {
this.name = name;
// 对应的图片ID
this.imageId = imageId;
} public String getName() {
return name;
} public int getImageId() {
return imageId;
} }

  (2)创建animal_item.xml文件,其中包含一个ImageView和一个TextView:

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

  (3)创建自定义适配器类AnimalAdapter,以Animal类为泛型,继承自ArrayAdapter<Animal>,重写父类的构造方法和getView方法,getView方法会在每个子项被滚动到屏幕内的时候调用:

 public class AnimalAdapter extends ArrayAdapter<Animal> {

     private int resourceId;

     public AnimalAdapter(Context context, int textViewResourceId,
List<Animal> objects) {
super(context, textViewResourceId, objects);
// textViewResourceId:ListView子项布局的id;objects:数据
resourceId = textViewResourceId;
} // getView方法会在每个子项被滚动到屏幕内的时候调用
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1.获取当前项的Animal实例
Animal animal = getItem(position); // 2.为这个子项加载传入的布局
View view = LayoutInflater.from(getContext()).inflate(resourceId, null); // 3.用view的findViewById方法获取到子项布局控件的实例
ImageView animalImage = (ImageView) view
.findViewById(R.id.animal_img_iv);
TextView animalName = (TextView) view.findViewById(R.id.animal_name_tv); // 4.设置相应控件的内容
animalImage.setImageResource(animal.getImageId());
animalName.setText(animal.getName()); // 5.返回view
return view;
}
}

  注:在getView方法里还可以为item的子控件添加点击事件。

  (4)MainActivity:

 public class MainActivity extends Activity {
// 1.创建动物名字数组和动物数据列表
private String[] animalNameList = { "猫", "狗", "狐狸", "小熊", "鱼", "老虎", "长颈鹿",
"象", "龙猫" };
private List<Animal> animalList = new ArrayList<Animal>();
// 为简单起见,把所有动物的图片都设置为ic_launcher
private int animalImageResourceId = R.drawable.ic_launcher; ListView listView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 2.初始化动物数据
initAnimals(); // 3.创建自定义的适配器实例
// 构造函数:第一个参数:当前上下文;第二个参数:子项布局xml文件;第三个参数:动物数据List
AnimalAdapter adapter = new AnimalAdapter(MainActivity.this,
R.layout.animal_item, animalList); // 4.获取ListView实例
listView = (ListView) findViewById(R.id.content_lv); // 5.设置适配器
listView.setAdapter(adapter); } private void initAnimals() {
for (int i = 0; i < animalNameList.length; i++) {
Animal animal = new Animal(animalNameList[i], animalImageResourceId);
animalList.add(animal);
}
}
}

  (5)运行效果:

  3、提升ListView的效率

  在AnimalAdapter的getView方法中,每次都将布局重新加载一遍,当快速滚动屏幕时候就会带来性能问题,为此要做一些优化。修改如下:

 public class AnimalAdapter extends ArrayAdapter<Animal> {

     private int resourceId;

     public AnimalAdapter(Context context, int textViewResourceId,
List<Animal> objects) {
super(context, textViewResourceId, objects);
// textViewResourceId:ListView子项布局的id;objects:数据
resourceId = textViewResourceId;
} // getView方法会在每个子项被滚动到屏幕内的时候调用
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Animal animal = getItem(position); // 用于提升性能
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.animalImage = (ImageView) view
.findViewById(R.id.animal_img_iv);
viewHolder.animalName = (TextView) view
.findViewById(R.id.animal_name_tv); // 将viewHolder存储在View中
view.setTag(viewHolder);
} else {
view = convertView; // 重新获取viewHolder
viewHolder = (ViewHolder) view.getTag();
} viewHolder.animalImage.setImageResource(animal.getImageId());
viewHolder.animalName.setText(animal.getName()); return view;
} // 创建内部类用于缓存,优化性能
class ViewHolder {
ImageView animalImage;
TextView animalName;
}
}

  4、为ListView的子项添加点击事件

  使用ListView对象的setOnItemClickListener方法,如:

      ...
listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Animal animal = animalList.get(position);
Toast.makeText(MainActivity.this, animal.getName(),
Toast.LENGTH_SHORT).show();
} });

  5、补充:

  (1)xml中设置ListView的分割线颜色:android:divider="#000"

  (2)将ListView定位到最后一行:listView.setSelection(dataList.size());

  (五)单位和尺寸

  1、像素密度:每英寸所包含的像素数,单位为dpi.

  x方向像素密度值的获取方法:float xdpi = getResources().getDisplayMetrics().xdpi;

  y方向像素密度值的获取方法:float ydpi = getResources().getDisplayMetrics().ydpi;

  2、使用dp为单位来设置控件的宽和高,就可以保证控件在不同像素密度的屏幕上显示的比例是一致的。使用sp来设置字体大小同理。

  

  (六)制作Nine-Patch图片

  详见另一篇博文:Android制作和使用Nine-Patch图片

随机推荐

  1. Extjs中设置只读的样式问题

    废话不多说,直接上代码:   view.down('#imageFile').hide(); view.down('#save_button').hide(); view.show(); view.d ...

  2. hadoop_百科

    一.发音是:[hædu:p]. 二.简介:Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储 ...

  3. K - Large Division 判断a是否是b的倍数。 a (-10^200 ≤ a ≤ 10^200) and b (|b| > 0, b fits into a 32 bit signed integer). 思路:取余;

    /** 题目:K - Large Division 链接:https://vjudge.net/contest/154246#problem/K 题意:判断a是否是b的倍数. a (-10^200 ≤ ...

  4. dropdown多选下拉框

    写好了一个dropdown多选框.直接粘下面代码就能用 效果展示: temp2.jsp <%@page import="com.util.LabelCacheManager" ...

  5. ThinkPHP与EasyUI整合之二(datagrid):删除多条记录

    学习EasyUI已有一段时间了,现在开始逐步把平时学习的细节和难点记录下来. 1. datagrid选中多条记录的语句是: var rows = $('#dg').datagrid('getSelec ...

  6. Web Services 概要

    WSDL WSDL 是基于 XML 的用来描述 Web services 以及如何访问它们的一种语言. WSDL 可描述 web service,连同用于 web service 的消息格式和协议的细 ...

  7. 百度-设置-搜索设置-每页显示50条-保存设置-打印alert信息-accept确定

    一.场景: 代码: #coding:utf-8from selenium import webdriverfrom selenium.webdriver.common.action_chains im ...

  8. JNI动态库生成、编译、查看相关简易资料

    有一篇好博文,大家可以看下http://www.cnblogs.com/zhangweia/archive/2010/09/16/1828176.html,我这里是参考其做的另外一个javah -cl ...

  9. Laravel5.1 Middleware中间件(初级)

    中间件?什么鬼? 大家第一次接触这个词都会有这么个疑问,但它其实没那么神秘. 一句话就可以解释它:过滤HTTP请求专用机制. 为什么要使用中间件? 过滤HTTP请求是可以写在别的地方,比如说控制器中 ...

  10. Dmidecode

    一.Dmidecode简介 DMI (Desktop Management Interface, DMI)就是帮助收集电脑系统信息的管理系统,DMI信息的收集必须在严格遵照SMBIOS规范的前提下进行 ...