《第一行代码》学习笔记 第 3 章

第 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() {
@Override
public void onClick(DialogInterface dialog, int which) {
……
}});
dialog.setNegativeButton("Cancel", new DialogInterface.
OnClickListener() {
@Override
public 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"可以达到自动适应屏幕宽度的作用。
<TableRow>
    <TextView
    android:layout_height="wrap_content"
    android:text="Account:" />  
    <EditText
    android:id="@+id/account"
    android:layout_height="wrap_content"
    android:hint="Input your account" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login" />
</TableRow>

四:自定义控件

  • 方法一:直接引入布局
<include layout="@layout/title" />
  • 方法二:创建自定义控件
  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. 在布局文件中添加这个自定义控件
<com.example.uicustomviews.TitleLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
></com.example.uicustomviews.TitleLayout>
  1. 为自定义的控件添加响应事件
Button titleBack = (Button) findViewById(R.id.title_back);
Button titleEdit = (Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public 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" };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//定义ArrayAdapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(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)
  • 制作聊天界面
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,860评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,072评论 25 709
  • Day1: 在代码中通过R.string.hello_world可以获得该字符串的引用; 在XML中通过@stri...
    冰凝雪国阅读 5,356评论 0 5
  • 本人初学Android,最近做了一个实现安卓简单音乐播放功能的播放器,收获不少,于是便记录下来自己的思路与知识总结...
    落日柳风阅读 19,347评论 2 41
  • 风勾起了思念 雨打开了记忆 不知几何 曾经有一个你 突然闯入 这宁静的时空 转身 看不到脸颊 浅浅的水痕 行走 听...
    零落霜结阅读 1,432评论 0 1

友情链接更多精彩内容