文档详情

创建用户界面.pdf

发布:2019-03-28约1.75万字共83页下载文档
文本预览下载声明
创建用户界面 创建用户界面 • View与ViewGroup • 可视化控件 • 布局 • 菜单 • 对话框 • 响应用户界面事件 • 用户界面的数据绑定 • 更改空间外观 View与ViewGroup • View是可视化控件(Widget)的基类 • 它主要提供了控件绘制和事件处理的方法。而可视化控 件,是指重新实现了View 的绘制和事件处理方法并最终 与用户交互的对象,如文本显示、按钮等。 • ViewGroup类继承自View类,是可视化Layout的 基类。 • 其最大的特点是可以有子控件。子控件是View类的对象, 当然也可以是ViewGroup类的对象,即ViewGroup可以 嵌套。 View与ViewGroup ViewGroup支持嵌套 可视化控件(Widgets ) Android本身提供了大量可视化控件 • TextView文件显示控件 • ImageView图像显示控件 • CheckBox复选控件 • RadioButton单选控件 • Button按钮 • ImageButton图像按钮 • EditText可编辑文件控件 • ToggleButton开关控件 • AnalogClock仿真时钟 • DigitalClock数字时钟 如何使用控件  在XML文件中静态设置 见Hello, World实例中的main.xml  在Java代码中动态设置 setContentView(R.layout.main); TextView tv = (TextView)this.findViewById(R.id.textview1); tv.setText(R.string.hello); View 的ID号  每个View对象都可以有一个整数ID号, 我们可以利用ID查询指定的View对象  定义一个ID号 Button android:id=@+id/my_button android:layout_width=wrap_content android:layout_height=wrap_content android:text=@string/my_button_text/ 查询ID:两个findViewById  public final View android.view.View.findViewById (int id)  public final View android.app.Activity.findViewById (int id) 使用不同的findViewById LinearLayout mainLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.main,null); TextView tv = (TextView)mainLayout.findViewById(R.id.textview1); tv.setText(R.string.hello); setContentView(mainLayout); 为什么LinearLayout能转化为TextView? TextView文本显示控件 TextView文本显示控件 ImageView图像显示控件 ImageView
显示全部
相似文档