安卓学习第二十一天:相对布局的使用,以及时间控件

来源:互联网 发布:人工智能技术推广方案 编辑:程序博客网 时间:2024/06/05 03:33

#1什么是相对布局

1,相对布局是另外一种控件摆放的方式

2,相对布局是通过指定当前控件与兄弟控件或者是父控件之间的相对位置,从而达到控制控件位置的目的

 

安卓布局嵌套得越多,这个程序的性能越差

默认放在左上角

 android:layout_toRightof把当前控件的边缘对齐到特定id控件的右边缘,值是一个id

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"

android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"

tools:context=".MainActivity">

 

<TextView

android:id="@+id/tx"

android:text="@string/hello_world"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#ff0000"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/tx"

android:text="第二个TextView"

android:background="#00ff00"/>

 

</RelativeLayout>

效果图:

计算机生成了可选文字: My Application - CD:XUsersXyuanyangXAndroidStudioProjectsXMyApplication21 - Cappl -  File Edit View Navigate Code Analyze Refector Build Run Tools VCS Window Help  MyApplication2 app src main  res layout activity_main.xml  ..xappxsrcxmainxresxlayoutxactivity_main.xml - Android Studio 1.2  w I Android  eaapp  manifests  drawable  layout  activity_main.xml  menu  mipmap  values  O Gradle Scripts  MainActivityjava x  activity_main.xml x  MainActivity •  Palette  4 • Nexus 4 •  APPT heme  My Applicati„  Component Tree  Device Screen  tx (TextView) - astring/hello world  TextView - "ZOTextView"  Fra me Layout  LinearLayout (Horizont  LinearLayout (Vertical)  TableLayout  GridLayout  RelativeLayout  Widgets  Plain TextView  Large Text  Medium Text  Small Text  Button  Small Button  O RadioButton  CheckBox  switch  ToggleButton  ImageButton  ImageView  ProgressBar (Large)  ProgressBar (Normal)  Desig n Text  Properties  layout:width  style  accessibilityLiveRegion  background  background Tint  background TintMode  clickable  content Descri ption  ma tch_pa re nt  ma tch_pa re nt  Event Log  Terminal 0: Messages •i' 6: Android TODO  Gradle build finished in 40s 788ms (7 minutes ago)  Gradle Console  n/a n/a  2015/5/5  Note

 

#2相对布局的最后一些属性

1android:layout_alignStart

2android:layout_alignEnd

3android:layout_alignParentStart

4android:layout_alignParentEnd

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

 

<TextView

android:id="@+id/tx"

android:text="登录界面"

android:gravity="center"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#ff0000"/>

<EditText

android:id="@+id/edtx"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="输入用户名"

android:layout_below="@id/tx"

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"/>

<EditText

android:id="@+id/edtx2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="输入密码"

android:inputType="textPassword"

android:layout_below="@id/edtx"

android:layout_alignLeft="@id/edtx"

android:layout_alignRight="@id/edtx"

 

/>

<Button

android:id="@+id/cancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="取消"

android:layout_below="@id/edtx2"

android:layout_alignRight="@id/edtx2"/>

 

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="确定"

android:layout_below="@id/edtx2"

android:layout_toLeftOf="@id/cancel"/>

 

 

</RelativeLayout>

 

#3时间与日期

1TimePicker与相应的监听器OntimeChangedListener

setIs24HourView设置使用24小时制

getCurrentHour()方法,获取当前时间

2DataPicker

里面不能使用监听器

通过OnClickListener来监听时间改变

java中的月份是从第0月开始算的

3AnalogClock模拟时钟

 

 

0 0
原创粉丝点击