布局文件采用相对布局:相对布局灵活,使用最频繁。常用的布局方式有:线性布局(水平或垂直),相对布局,绝对布局,网格布局,表格布局,列表布局,标签布局,其中用的最多是相对布局,其次为网格布局(图片浏览),标签布局(多页面切换)等。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/MyButon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#FF0000"
android:textSize="25sp"
/>
<Button
android:id="@+id/MyButon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/MyButon1"
android:textColor="#FF0000"
android:textSize="25sp"
/>
<Button
android:id="@+id/MyButon3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/MyButon2"
android:textColor="#FF0000"
android:textSize="25sp"
/>
<Button
android:id="@+id/MyButon4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/MyButon3"
android:textColor="#FF0000"
android:textSize="25sp"
/>
<Button
android:id="@+id/MyButon5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/MyButon4"
android:textColor="#FF0000"
android:textSize="25sp"
/>
<TextView
android:id="@+id/MytextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#008888"
android:textSize="18sp"
android:layout_below="@+id/MyButon1"
android:layout_alignParentLeft="true"
/>
</RelativeLayout >
字符变量为:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">天机测算</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="mytextview">hello world,The is my first Textview on 2014-5-3
</string>
<string name="Mybutton">button1</string>
</resources>
java源代码为:
package com.example.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1=(Button)findViewById(R.id.MyButon1);
Button bt2=(Button)findViewById(R.id.MyButon2);
Button bt3=(Button)findViewById(R.id.MyButon3);
Button bt4=(Button)findViewById(R.id.MyButon4);
Button bt5=(Button)findViewById(R.id.MyButon5);
bt1.setText(" A ");
bt2.setText(" B ");
bt3.setText(" C ");
bt4.setText(" D ");
bt5.setText(" E ");
bt1.setOnClickListener(listener);
bt2.setOnClickListener(listener);
bt3.setOnClickListener(listener);
bt4.setOnClickListener(listener);
bt5.setOnClickListener(listener);
TextView tv=(TextView)findViewById(R.id.MytextView);
tv.setText("\t一语破天机,看看你今天的运气如何?");
}
private OnClickListener listener =new OnClickListener()
{
@Override
public void onClick(View v) {
Button btx=(Button)v;
TextView tv=(TextView)findViewById(R.id.MytextView);
switch(btx.getId())
{
case R.id.MyButon1:
Toast.makeText(MainActivity.this, "运气一般", Toast.LENGTH_LONG).show();
tv.setText("\t恩,你今天运气一般,请踏实工作,真诚待人,戒骄戒躁,赚点人品吧!");
break;
case R.id.MyButon2:
Toast.makeText(MainActivity.this, "人品爆发", Toast.LENGTH_LONG).show();
tv.setText("\t哇塞,你今天运气超好,建议去买张彩票,一夜逆袭机会来了!");
break;
case R.id.MyButon3:
Toast.makeText(MainActivity.this, "运交华盖", Toast.LENGTH_LONG).show();
tv.setText("\t哦,你今天运交华盖,建议不要出门了,小心为妙!");
break;
case R.id.MyButon4:
Toast.makeText(MainActivity.this, "运气还不错", Toast.LENGTH_LONG).show();
tv.setText("\t嗨,今天走路注意看地面,前面会有人掉钱包,钱包没钱,只有一张公交卡!");
break;
case R.id.MyButon5:
Toast.makeText(MainActivity.this, "运气有点背", Toast.LENGTH_LONG).show();
tv.setText("\t晕,你今天心神不宁,可能会丢三落四,出门记得带好钥匙和钱包!");
break;
}
}
};
}
编译后产生的apk生成的图标为:
软件界面:
测试场景一: