任务1:使用线性布局实现如下界面的设计
代码:
修改activity_main.xml布局文件,将RelativetyLayoutgaiwei 改为LinearyLayout布局编码
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="1"
android:background="@android:color/black" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="1"
android:background="@android:color/darker_gray" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="1"
android:background="@color/blue" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="1"
android:background="@color/green" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="1"
android:background="@color/red" />
任务2:使用相对布局实现如下界面的设计
代码:
activity_main.xml布局文件代码RelativeLayout编码
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label"
android:background="@android:drawable/editbox_background"
/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="Ok"
/>
<Button
android:id="@+id/cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/ok"
android:layout_toLeftOf="@id/ok"
android:text="cancel"
/>
MainActivity.java编码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
//获取id,系统方法
public boolean onOptionsItemSelected(MenuItem item){
int id=item.getItemId();
switch(id){
case R.id.about:
Toast.makeText(MainActivity.this, "作者:小不点\n时间:2013-3-26",Toast.LENGTH_LONG).show(); //显示提示讯息
break;
case R.id.exit:
MainActivity.this.finish();
break;
}
return true;
}
}
小结:学会使用三种布局文件,这样才能使你的页面更加漂亮