《第一行代码》学习笔记 第 3 章
16lz
2021-01-26
第 3 章 UI 开发的点点滴滴
一:ProgressBar
- 默认为旋转进度条
- 修改为水平进度条
- 在布局中修改进度条控件设置
style="?android:attr/progressBarStyleHorizontal"android:max="100"
- 在代码中动态地更改进度条的进度
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)
- 最外层布局为linearlayout(android:orientation="horizontal" )
- 内部控件分别为
android:layout_gravity="top" android:layout_gravity="center_vertical" android:layout_gravity="bottom"
- (“器”字型布局)RelativeLayout
- 最外层布局为RelativeLayout
- 内部控件为(相对于全局)
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" - 或者为(相对于其他控件)
android:layout_above="@id/button3"
android:layout_toLeftOf="@id/button3"
- FrameLayout(略)
- TableLayout(可用于登录界面)
在最外层布局中加入android:stretchColumns="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); }}
- 在布局文件中添加这个自定义控件
- 为自定义的控件添加响应事件
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 的界面
- 定义一个实体类 Fruit
- 为 ListView 的子项指定一个我们自定义的布局,在 layout 目录下新建fruit_item.xml
- 创建一个自定义的适配器,这个适配器继承自ArrayAdapter,并将泛型指定为 Fruit类.
- 在代码中设置list、adapter、和listview
- 提升代码效率(设置viewholder)
- 制作聊天界面
更多相关文章
- android中ViewPager 与fragment 的一个应用
- android ListView 点击无响应
- Layout_weight的特性
- 安卓入门.控件样式
- Android(安卓)5.0系统 style 默认窗口控件颜色值设置
- Android(安卓)Studio 安装使用教程
- android 布局相关
- android 使用Google地图步骤要点
- Android(安卓)中文 API 文档 (45) —— AbsoluteLayout.LayoutPara