Android开发中关于Activity和布局的语句
Activity中的基本语句
Android程序的设计讲究逻辑和视图的分离,布局用来显示界面内容
布局文件样例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"//活动的宽度
android:layout_height="match_parent">//活动的高度
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
</LinearLayout>
Toast通知
Toast.makeText(FirstActivity.this,"提示信息",Toast.LENGTH_SHORT).show();
Button按下启动活动
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//添加活动
}
});
活动中使用Menu
1.添加menu文件夹,创建main资源文件
<item
android:id="@+id/add_item"
android:title="选项名称"/>
2.在活动中重写onCreateOptionsMenu()方法
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
3.定义菜单响应事件
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.“选项id”:
//响应事件
break;
}
}
使用Intent在活动中进行穿梭
- 显式Intent
//点击Button后触发的操作
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
//点击Button后跳转到SecondActivity
- 隐式Intent
1.在AndroidManifest.xml中配置ACTION和CATEGORY
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
2.在活动中修改点击事件
Intent intent = new Intent("在XML文件中配置的ACTION标签");
intent.addCategory("在XML文件中配置的CATEGORY标签");
startActivity(intent);
与UI有关的语句
这一部分都是在layout文件中进行配置,用来设置Android界面
TextView
用于在界面显示一段文本信息
<TextView
android:id="@+id=/text_view"
android:layout_width="match_parebt"
android:layout_height="wrap_content"
android:text="文本信息内容"
/>
Button
程序用于和用户进行交互的重要控件
<Button
android:id="@id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮上的文字"
/>
EditText
程序用于和用户进行交互的控件,它允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理。
<EditText
android:id="+@id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_conten"
android:hint="Type something here"//提示性文字
android:maxLines="3"//指定输入最大显示行数为3行
/>
Button和EditText之间结合使用
在使用此布局文件的活动中定义响应活动
private EditText editText;
protected void onCreat(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);//通过id找到按钮
editText=(EditText)findViewById(R.id.edit_text);//通过id找到Edit的编辑框
button.setOnClickListener(this);//通过点击Button触发事件
}
@override
public void onClick(View v){
switch(v.getId()){
case R.id.button:
String inputText = editText.getText().toString();//将editText中的文字转换成String
Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show();//通过Toast消息提醒文本内容
break;
default:
break;
}
}
ImageView
ImageView是用于在界面上展示图片的一个控件,图片放在以"drawable"开头的目录下,由于预设的drawable中没有指明分辨率,所以需要在res目录创建"drawable-xhdpi"目录,其中xhdpi指图片的分辨率
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img_1"//指明图片地址
/>
同样如同Button和EditText的交互一样,同样可以在活动中设置点击按钮实现换图片的操作
private ImageView imageView;
protected void onCreate(Bundle saveInstanceState){
//....
imageView = (ImageView)findviewById(R.id.image_view);
//....
}
public void onClick(View v){
switch(v.getId()){
case R.id.button:
imageView.setImageResource(R.drawable.img_2);
break;
default:
break;
}
}
关于布局
线性布局(LinearLayout)
这个布局会将它所包含的控件在线性方向上依次排列,线性包括水平线性和垂直线性,通过修改layout文件中的android:orientation=“ ”实现中可“ ”中的值可以为vertical(垂直线性)和horizontal(水平线性)
-
android:layout_weight属性
这个属性允许我们使用比例的方式来指定控件的大小。
//布局为水平线性 <EditText ... android:layout_weight="1" ... /> <Button ... android:layout_weight="1" ... /> //此时两个控件在水平上平分宽度
相对布局(RelativeLayout)
相对布局通过相对定位的方式让控件出现在布局的任何位置。
以下通过标签可以控制控件的绝对位置
android:layout_alignParentLeft=”true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
以下标签可以控制各控件之间的之间的相对位置
android:layout_above="@id/控件id名"//在某控件之上
android:layout_below="@id/控件id名"//在某控件之下
android:layout_toLeftof="@id/控件id名"//在某控件之左
android:layout_toRightof="@id/控件id名"//在某控件之右