第一行代码笔记3
来源:互联网 发布:linux怎么安装输入法 编辑:程序博客网 时间:2024/05/17 04:59
AlertDialog:
public void onClick(View v)
{ switch (v.getId())
{ case R.id.button: 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(); break; default: break;
}
}
android:gravity是用 于指定文字在控件中的对齐方式,而 android:layout_gravity是用于指定控件在布局中的对齐 方式
由于目前 LinearLayout的排列方向是 horizontal,因此我们只能指定垂直方向上的排列方 向,将第一个Button的对齐方式指定为top,第二个Button的对齐方式指定为center_vertical, 第三个 Button的对齐方式指定为 bottom
:
RelativeLayout:
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button 2" />
。android:layout_above属性可以让 一个控件位于另一个控件的上方,需要为这个属性指定相对控件 id的引用,这里我们填入 了@id/button3,表示让该控件位于 Button 3 的上方。其他的属性也都是相似的, android:layout_below表示让一个控件位于另一个控件的下方,android:layout_toLeftOf表示让 一个控件位于另一个控件的左侧,android:layout_toRightOf表示让一个控件位于另一个控件 的右侧。注意,当一个控件去引用另一个控件的 id时,该控件一定要定义在引用控件的后 面,不然会出现找不到 id的情况。重新运行程序,效果如图 3.22所示
TableLayout
TableLayout允许我们使用表格的方式来排列控件;
引入布局:
现在标题栏布局已经编写完成了,剩下的就是如何在程序中使用这个标题栏了,修改 activity_main.xml中的代码,如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/title" />
</LinearLayout>
创建自定义控件:
public class TitleLayout extends LinearLayout
{
public TitleLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
}
}
首先我们重写了 LinearLayout中的带有两个参数的构造函数,在布局中引入 TitleLayout 控件就会调用这个构造函数。然后在构造函数中需要对标题栏布局进行动态加载,这就要借 助 LayoutInflater来实现了。通过 LayoutInflater的 from()方法可以构建出一个 LayoutInflater 对象,然后调用 inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参数,第 一个参数是要加载的布局文件的 id,这里我们传入 R.layout.title,第二个参数是给加载好的 布局再添加一个父布局,这里我们想要指定为 TitleLayout,于是直接传入 this
现在自定义控件已经创建好了,然后我们需要在布局文件中添加这个自定义控件,修改 activity_main.xml中的代码,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.uicustomviews.TitleLayout>
</LinearLayout>
添加自定义控件和添加普通控件的方式基本是一样的,只不过在添加自定义控件的时候 我们需要指明控件的完整类名,包名在这里是不可以省略的。
- 第一行代码笔记3
- 第一行代码笔记3
- 第一行代码-笔记
- 《第一行代码》笔记
- 第一行代码笔记
- 第一行代码笔记3:数据存储
- 《Android第一行代码》笔记
- 《第一行代码Android》笔记
- 第一行代码笔记1
- 第一行代码笔记2
- 读《第一行代码》笔记
- 第一行代码笔记二
- 《第一行代码--android》笔记
- android第一行代码笔记
- <第一行代码>笔记一
- <第一行代码>笔记二
- <第一行代码>笔记三
- 第一行代码前四章笔记。
- 网络编程之TCP协议
- C++ const char* 常量字符串拼接问题
- 自定义ProgressDialog
- 安卓广播
- Day47:CSA meeting
- 第一行代码笔记3
- 使用控件时提示"未声明标识符"的解决方法
- 【Oracle】查询和运算符
- Spring 使用注解方式进行事务管理
- 第九周 OJ-4
- CentOS7安装nginx及配置
- B/S架构与C/S架构的区别
- Sdelayer注册
- java IO流学习总结