第 3 章 UI 开发的点点滴滴

一:ProgressBar

  • 默认为旋转进度条
  • 修改为水平进度条
    1. 在布局中修改进度条控件设置
style="?android:attr/progressBarStyleHorizontal"android:max="100"
  1. 在代码中动态地更改进度条的进度
int progress = progressBar.getProgress();progress = progress + 10;progressBar.setProgress(progress);

二:AlertDialog

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);dialog.setTitle("This is Dialog");dialog.setMessage("Something important.");dialog.setCancelable(false);dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {……}});dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {……}});dialog.show();

三:布局

  • 楼梯型布局(LinearLayout)
  1. 最外层布局为linearlayout(android:orientation="horizontal" )
  2. 内部控件分别为
    android:layout_gravity="top"    android:layout_gravity="center_vertical"    android:layout_gravity="bottom"
  • (“器”字型布局)RelativeLayout
  1. 最外层布局为RelativeLayout
  2. 内部控件为(相对于全局)
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
  3. 或者为(相对于其他控件)
    android:layout_above="@id/button3"
    android:layout_toLeftOf="@id/button3"
  • FrameLayout(略)
  • TableLayout(可用于登录界面)
    在最外层布局中加入android:stretchColumns="1"可以达到自动适应屏幕宽度的作用。
          

四:自定义控件

  • 方法一:直接引入布局
  • 方法二:创建自定义控件
  1. 新建 TitleLayout 继承自 LinearLayout
public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs) {    super(context, attrs);    LayoutInflater.from(context).inflate(R.layout.title, this);    }}   
  1. 在布局文件中添加这个自定义控件
  1. 为自定义的控件添加响应事件
Button titleBack = (Button) findViewById(R.id.title_back);Button titleEdit = (Button) findViewById(R.id.title_edit);titleBack.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {((Activity) getContext()).finish();}});titleEdit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getContext(), "You clicked Edit button",Toast.LENGTH_SHORT).show();}});}}

五:最常用和最难用的控件——ListView

  • 基本应用(单纯显示文本)
private String[] data = { "Apple", "Banana", "Orange", "Watermelon","Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);//定义ArrayAdapter    ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1, data);//查找listView     ListView listView = (ListView) findViewById(R.id.list_view);//setadapter    listView.setAdapter(adapter);}
  • 定制 ListView 的界面
  1. 定义一个实体类 Fruit
  2. 为 ListView 的子项指定一个我们自定义的布局,在 layout 目录下新建fruit_item.xml
  3. 创建一个自定义的适配器,这个适配器继承自ArrayAdapter,并将泛型指定为 Fruit类.
  4. 在代码中设置list、adapter、和listview
  5. 提升代码效率(设置viewholder)
  • 制作聊天界面

更多相关文章

  1. android中ViewPager 与fragment 的一个应用
  2. android ListView 点击无响应
  3. Layout_weight的特性
  4. 安卓入门.控件样式
  5. Android(安卓)5.0系统 style 默认窗口控件颜色值设置
  6. Android(安卓)Studio 安装使用教程
  7. android 布局相关
  8. android 使用Google地图步骤要点
  9. Android(安卓)中文 API 文档 (45) —— AbsoluteLayout.LayoutPara

随机推荐

  1. Android(安卓)Studio 1.5.1更新说明与下
  2. Android(安卓)TTS学习――TTS初体验(中)
  3. Android(安卓)序列化
  4. Android(安卓)绘制折线和柱状图
  5. C#、Golang、Python、Java(Android)之间Des
  6. Android(安卓)sdk version and API level
  7. 仅layout使用继承TextView,报java.lang.Nu
  8. 2011.06.17)——— android MotionEvent中
  9. android内存溢出的解决方法
  10. android 支持的Html标签