- 浏览: 410809 次
-
文章分类
最新评论
-
tedeum:
美帝人民不为钱写代码是因为万恶的资本主义社会保障制度
读《20个月赚130亿》有感 -
novembersky:
还好你没相信那个大饼
谁坑了程序员的职业道德?送给初入职的码农,和宣传奴性的领导。 -
禀圣含华:
也不要着急进一家公司,就因为那公司急着招人。。急着招人背后又是 ...
谁坑了程序员的职业道德?送给初入职的码农,和宣传奴性的领导。 -
haohao-xuexi02:
每个码奴辛辛苦苦最后,还不能干脆的走。。各种被坑。。。
找工作 ...
谁坑了程序员的职业道德?送给初入职的码农,和宣传奴性的领导。 -
hot002:
BuN_Ny 写道真实项目中的6种人:1.怂恿者2.批评者3. ...
一个优秀创业团队需要的6种人
主题:三,android编码规范 & 常用布局 & 常用控件
1,android编码规范
Android官方并没有给出相应编码规范。以下都是我从源码、示例中总结的所谓规范。若公司有相应规范以公司为准。
首先从布局文件说起,布局文件名称的定义必须为小写字母,否者无法生成R类,尽量不要用缩写。以表达清楚该文件用途为本,通常情况下用下划线连接各语义单词,例如dialog_title_icons.xml或者list_menu_item_checkbox.xml。
控件ID的定义,ID的定义一律为小写,例如:一用户名TextView可以定义为:@+id/username_view。以“名词_控件名称”这种形式定义。
其次是图片的定义格式,图片的定义也是为解释清楚用途为准,参照这种定义格式“btn_background_ok.png”
string类的name定义,这里可以按照JAVA中变量的定义方式定义。首字母小写,驼峰命名法。例 如: <stringname="userName_view">用户名:</string>
最后类名与变量的定义,定义与用户交互的类,××Activity.java。自定义变量一律以小写m开头例如:EditTextmUserName=(EditText)findViewById(R.id.username_edit);
2,常用布局
Android提供了一组视图类来充当视图的容器,这些容器类称为布局或者布局管理器,每一个都实现一种具体的策略来管理其子控件的大小和位置。最常用的布局有以下这几种:
LinearLayout,RleativeLayout,TableLayout,FrameLayout等。有两种方式可以声明布局,一种是编码的方式,另外一中通过XML配置的方式。Android默认是通过xml的方式构建应用程序的。这种方式最大的优点是代码与视图分离,这意味着你可以修改或调整,而无需修改源代码并重新编译。例如你可以创建不同的屏幕方向,支持不同分辨率的设备。也更直观更容易调试。
(1)LinearLayout:线性布局
最常用的一种布局方式,所有子控件的对齐方式,取决于如何定义orientation的属性:vertical 垂直方向,如果按照这种方向所有的子控件将按照垂直的方式分布在布局上,每行只允许有 一 个子元素,horizontal水平方向,这时子控件将会以水平的方向分布在布局中。以下线性布 局的简单例子。先上图:
- <?xmlversion="1.0"encoding="utf-8"?>
- <!--线性布局,最外面包裹一个水平线性布局-->
- <!--orientation表示线性布局的方向,horizontal:水平方向vertical:垂直方向-->
- <!--@代表R类,如果是自定义的ID则用@+id/×××表示,如果是引用R类的资源则@string/×××-->
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:background="@drawable/bg"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/linear"
- />
- <Button
- android:id="@+id/button"
- android:layout_width="183dp"
- android:layout_height="wrap_content"
- android:text="@string/button"
- />
- <ImageButton
- android:id="@+id/imagebutton"
- android:layout_width="180dp"
- android:layout_height="48dp"
- android:src="@drawable/imagebutton"
- />
- </LinearLayout>
- <!--android:layout_gravity与android:gravity区别,拿一个button作为例子
- 前者的意思,是这个按钮的位置,如果设置为right则表示这个按钮整体位置靠右;
- 后者的意思,这个按钮上显示内容的位置。
- -->
- <LinearLayout
- android:gravity="right"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <ImageView
- android:id="@+id/imageview"
- android:layout_marginTop="5dp"
- android:src="@drawable/imageview"
- android:layout_width="131dp"
- android:layout_height="131dp"
- />
- </LinearLayout>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <!--线性布局,最外面包裹一个水平线性布局--> <!--orientation表示线性布局的方向,horizontal:水平方向 vertical:垂直方向 --> <!-- @代表R类,如果是自定义的ID 则用@+id/××× 表示,如果是引用R类的资源则@string/×××--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:background="@drawable/bg" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/linear" /> <Button android:id="@+id/button" android:layout_width="183dp" android:layout_height="wrap_content" android:text="@string/button" /> <ImageButton android:id="@+id/imagebutton" android:layout_width="180dp" android:layout_height="48dp" android:src="@drawable/imagebutton" /> </LinearLayout> <!-- android:layout_gravity与android:gravity区别,拿一个button作为例子 前者的意思,是这个按钮的位置,如果设置为right则表示这个按钮整体位置靠右; 后者的意思,这个按钮上显示内容的位置。 --> <LinearLayout android:gravity="right" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/imageview" android:layout_marginTop="5dp" android:src="@drawable/imageview" android:layout_width="131dp" android:layout_height="131dp" /> </LinearLayout> </LinearLayout>
package net.csdn.blog.androidtoast;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Toast;
- publicclassMainActivityextendsActivityimplementsOnClickListener{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.linearlayout);
- //实例化以下控件,并设置监听事件,传入实现了OnClickListener接口的对象
- findViewById(R.id.button).setOnClickListener(this);
- findViewById(R.id.imagebutton).setOnClickListener(this);
- findViewById(R.id.imageview).setOnClickListener(this);
- }
- /**
- *点击事件判断所点击是哪个控件并toast提示。
- */
- @Override
- publicvoidonClick(Viewv){
- intid=v.getId();//得到所点对象ID
- if(id==R.id.button){
- Toast.makeText(getApplicationContext(),R.string.promptButton,1).show();
- }elseif(id==R.id.imagebutton){
- Toast.makeText(getApplicationContext(),R.string.promptImageButton,1).show();
- }elseif(id==R.id.imageview){
- Toast.makeText(getApplicationContext(),R.string.promptImageView,1).show();
- }
- }
- }
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.linearlayout); //实例化以下控件,并设置监听事件,传入实现了OnClickListener接口的对象 findViewById(R.id.button).setOnClickListener(this); findViewById(R.id.imagebutton).setOnClickListener(this); findViewById(R.id.imageview).setOnClickListener(this); } /** * 点击事件判断所点击是哪个控件并toast提示。 */ @Override public void onClick(View v) { int id=v.getId();//得到所点对象ID if(id==R.id.button){ Toast.makeText(getApplicationContext(), R.string.promptButton, 1).show(); }else if(id==R.id.imagebutton){ Toast.makeText(getApplicationContext(), R.string.promptImageButton, 1).show(); }else if(id==R.id.imageview){ Toast.makeText(getApplicationContext(), R.string.promptImageView, 1).show(); } } }
(2)RleativeLayout:相对布局
如果你的程序中出现了多个LinearLayout嵌套,就应该考虑使用相对布局了。相对局部顾名思义一个控件的位置相对于其他控件或者容器的位置。使用很简单直接上示例:
- <?xmlversion="1.0"encoding="utf-8"?>
- <!--相对布局一个控件相对于另一个控件或者容器的位置。-->
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="@drawable/bg"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/describe_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:textColor="#556055"
- />
- <!--这个TextView相对于上一个TextView在它的下方所以设置属性为layout_below-->
- <TextView
- android:id="@+id/username_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="12dp"
- android:text="@string/username"
- android:textColor="#556055"
- android:layout_below="@id/describe_view"
- />
- <EditText
- android:id="@+id/username_edit"
- android:layout_width="90dp"
- android:layout_height="40dp"
- android:layout_marginTop="4dp"
- android:layout_toRightOf="@id/username_view"
- android:layout_below="@id/describe_view"
- />
- <TextView
- android:id="@+id/sex_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="12dp"
- android:text="@string/sex"
- android:textColor="#556055"
- android:layout_below="@id/describe_view"
- android:layout_toRightOf="@id/username_edit"
- />
- <RadioGroup
- android:id="@+id/sex_radiogroup"
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/sex_view"
- android:layout_below="@id/describe_view"
- >
- <!--第一个RadioButton-->
- <RadioButton
- android:id="@+id/male_radiobutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="男"
- />
- <!--第二个RadioButton-->
- <RadioButton
- android:id="@+id/woman_radiobutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="女"
- />
- </RadioGroup>
- <TextView
- android:id="@+id/age_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="25dp"
- android:text="@string/age"
- android:textColor="#556055"
- android:layout_below="@id/username_view"
- />
- <EditText
- android:id="@+id/brithday_edit"
- android:layout_width="90dp"
- android:layout_height="40dp"
- android:layout_marginTop="4dp"
- android:hint="@string/selectdate"
- android:textSize="13sp"
- android:gravity="center"
- android:editable="false"
- android:layout_toRightOf="@id/age_view"
- android:layout_below="@id/username_edit"
- />
- <TextView
- android:id="@+id/education_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="25dp"
- android:text="@string/education"
- android:textColor="#556055"
- android:layout_below="@id/sex_view"
- android:layout_toRightOf="@id/brithday_edit"
- />
- <!--下拉列表控件-->
- <Spinner
- android:id="@+id/edu_spinner"
- android:layout_width="108dp"
- android:layout_height="38dp"
- android:layout_below="@id/sex_radiogroup"
- android:layout_toRightOf="@id/education_view"
- />
- <TextView
- android:id="@+id/interest_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="25dp"
- android:text="@string/interest"
- android:textColor="#556055"
- android:layout_below="@id/age_view"
- />
- <!--复选框控件-->
- <CheckBox
- android:id="@+id/car_check"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/car"
- android:textColor="#566156"
- android:layout_toRightOf="@id/interest_view"
- android:layout_below="@id/brithday_edit"
- />
- <CheckBox
- android:id="@+id/sing_check"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="11dp"
- android:text="@string/sing"
- android:textColor="#566156"
- android:layout_toRightOf="@id/car_check"
- android:layout_below="@id/brithday_edit"
- />
- <CheckBox
- android:id="@+id/movie_check"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="11dp"
- android:text="@string/movie"
- android:textColor="#566156"
- android:layout_toRightOf="@id/sing_check"
- android:layout_below="@id/brithday_edit"
- />
- <Button
- android:id="@+id/submit_button"
- android:layout_width="100dp"
- android:layout_height="40dp"
- android:text="@string/submit"
- android:gravity="center"
- android:layout_below="@id/movie_check"
- android:layout_marginLeft="210dp"
- android:layout_marginTop="15dp"
- />
- </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <!-- 相对布局 一个控件相对于另一个控件或者容器的位置。 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/bg" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/describe_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="#556055" /> <!-- 这个TextView相对于上一个TextView 在 它的下方所以设置属性为layout_below--> <TextView android:id="@+id/username_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="@string/username" android:textColor="#556055" android:layout_below="@id/describe_view" /> <EditText android:id="@+id/username_edit" android:layout_width="90dp" android:layout_height="40dp" android:layout_marginTop="4dp" android:layout_toRightOf="@id/username_view" android:layout_below="@id/describe_view" /> <TextView android:id="@+id/sex_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="@string/sex" android:textColor="#556055" android:layout_below="@id/describe_view" android:layout_toRightOf="@id/username_edit" /> <RadioGroup android:id="@+id/sex_radiogroup" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/sex_view" android:layout_below="@id/describe_view" > <!--第一个RadioButton --> <RadioButton android:id="@+id/male_radiobutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> <!--第二个RadioButton --> <RadioButton android:id="@+id/woman_radiobutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <TextView android:id="@+id/age_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="25dp" android:text="@string/age" android:textColor="#556055" android:layout_below="@id/username_view" /> <EditText android:id="@+id/brithday_edit" android:layout_width="90dp" android:layout_height="40dp" android:layout_marginTop="4dp" android:hint="@string/selectdate" android:textSize="13sp" android:gravity="center" android:editable="false" android:layout_toRightOf="@id/age_view" android:layout_below="@id/username_edit" /> <TextView android:id="@+id/education_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="25dp" android:text="@string/education" android:textColor="#556055" android:layout_below="@id/sex_view" android:layout_toRightOf="@id/brithday_edit" /> <!-- 下拉列表控件 --> <Spinner android:id="@+id/edu_spinner" android:layout_width="108dp" android:layout_height="38dp" android:layout_below="@id/sex_radiogroup" android:layout_toRightOf="@id/education_view" /> <TextView android:id="@+id/interest_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="25dp" android:text="@string/interest" android:textColor="#556055" android:layout_below="@id/age_view" /> <!-- 复选框控件 --> <CheckBox android:id="@+id/car_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/car" android:textColor="#566156" android:layout_toRightOf="@id/interest_view" android:layout_below="@id/brithday_edit" /> <CheckBox android:id="@+id/sing_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="11dp" android:text="@string/sing" android:textColor="#566156" android:layout_toRightOf="@id/car_check" android:layout_below="@id/brithday_edit" /> <CheckBox android:id="@+id/movie_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="11dp" android:text="@string/movie" android:textColor="#566156" android:layout_toRightOf="@id/sing_check" android:layout_below="@id/brithday_edit" /> <Button android:id="@+id/submit_button" android:layout_width="100dp" android:layout_height="40dp" android:text="@string/submit" android:gravity="center" android:layout_below="@id/movie_check" android:layout_marginLeft="210dp" android:layout_marginTop="15dp" /> </RelativeLayout>
(3)TableLayout:表格布局
TableLayout布局是LinearLayout的扩展,以行和列的形式组织其子控件。与HTML中得Table相似。每一个TableRow元素代表一行。TableRow中包含几个控件代表几列。尽管使用TableRow来填充TableLayout是最常见的模式,但是该布局中可以放置任何子控件。需要指出的是TableLayout的子控件不能指定android:layout_width="wrap_content",它们被强制设定为fill_parent。但是可以设置高度。还有两个不太好理解的属性的说一下,android:stretchColums此属性指要被拉伸的列。取值可以单个列的索引也可以是一组列的索引值。例如:如果一行有三列。stretchColums="1"这表示拉伸第二列填充剩余空间。android:layout_column="1"这个属性指定子控件放置在哪一列上。例如
<TextViewandroid:layout_column="1"android:text="Open..."android:padding="3dip"/>指该控放置在第二列。上图:
- <?xmlversion="1.0"encoding="utf-8"?>
- <!--
- android:stretchColumns="1"是设置TableLayout所有行的第二列为拉伸列。
- 也就是说如果每行都有三列的话,剩余的空间由第二列补齐
- -->
- <!--最外层包裹一个滚动条-->
- <ScrollView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TableLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:stretchColumns="1">
- <TableRow>
- <View
- android:layout_height="80dp"
- android:layout_width="250dp"
- android:background="#D6D6D6"
- />
- <TextView
- android:layout_height="80dp"
- android:text="#D6D6D6"
- android:gravity="center"/>
- </TableRow>
- <!--此处的View控件充当着一个分割条的作用-->
- <View
- android:layout_height="2dip"
- android:background="#C4C4C4"/>
- <TableRow>
- <View
- android:layout_height="80dp"
- android:layout_width="250dp"
- android:background="#292929"
- />
- <TextView
- android:layout_height="80dp"
- android:text="#292929"
- android:gravity="center"/>
- </TableRow>
- <View
- android:layout_height="2dip"
- android:background="#C4C4C4"/>
- <TableRow>
- <View
- android:layout_height="80dp"
- android:layout_width="250dp"
- android:background="#8A500E"
- />
- <TextView
- android:layout_height="80dp"
- android:text="#8A500E"
- android:gravity="center"/>
- </TableRow>
- <View
- android:layout_height="2dip"
- android:background="#C4C4C4"/>
- <TableRow>
- <View
- android:layout_height="80dp"
- android:layout_width="250dp"
- android:background="#D67C15"
- />
- <TextView
- android:layout_height="80dp"
- android:text="#D67C15"
- android:gravity="center"/>
- </TableRow>
- <View
- android:layout_height="2dip"
- android:background="#C4C4C4"/>
- <TableRow>
- <View
- android:layout_height="80dp"
- android:layout_width="250dp"
- android:background="#8A270E"
- />
- <TextView
- android:layout_height="80dp"
- android:text="#8A270E"
- android:gravity="center"/>
- </TableRow>
- <View
- android:layout_height="2dip"
- android:background="#C4C4C4"/>
- <TableRow>
- <View
- android:layout_height="80dp"
- android:layout_width="250dp"
- android:background="#D63C16"
- />
- <TextView
- android:layout_height="80dp"
- android:text="#D63C16"
- android:gravity="center"/>
- </TableRow>
- </TableLayout>
- </ScrollView>
<?xml version="1.0" encoding="utf-8"?> <!-- android:stretchColumns="1"是设置 TableLayout所有行的第二列为拉伸列。 也就是说如果每行都有三列的话,剩余的空间由第二列补齐 --> <!-- 最外层包裹一个滚动条 --> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <View android:layout_height="80dp" android:layout_width="250dp" android:background="#D6D6D6" /> <TextView android:layout_height="80dp" android:text="#D6D6D6" android:gravity="center"/> </TableRow> <!-- 此处的View控件充当着一个分割条的作用 --> <View android:layout_height="2dip" android:background="#C4C4C4" /> <TableRow> <View android:layout_height="80dp" android:layout_width="250dp" android:background="#292929" /> <TextView android:layout_height="80dp" android:text="#292929" android:gravity="center"/> </TableRow> <View android:layout_height="2dip" android:background="#C4C4C4" /> <TableRow> <View android:layout_height="80dp" android:layout_width="250dp" android:background="#8A500E" /> <TextView android:layout_height="80dp" android:text="#8A500E" android:gravity="center"/> </TableRow> <View android:layout_height="2dip" android:background="#C4C4C4" /> <TableRow> <View android:layout_height="80dp" android:layout_width="250dp" android:background="#D67C15" /> <TextView android:layout_height="80dp" android:text="#D67C15" android:gravity="center"/> </TableRow> <View android:layout_height="2dip" android:background="#C4C4C4" /> <TableRow> <View android:layout_height="80dp" android:layout_width="250dp" android:background="#8A270E" /> <TextView android:layout_height="80dp" android:text="#8A270E" android:gravity="center"/> </TableRow> <View android:layout_height="2dip" android:background="#C4C4C4" /> <TableRow> <View android:layout_height="80dp" android:layout_width="250dp" android:background="#D63C16" /> <TextView android:layout_height="80dp" android:text="#D63C16" android:gravity="center"/> </TableRow> </TableLayout> </ScrollView>
(4)FrameLayout:帧布局
最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象—比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。但是你可以通过子控件自身控制其位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。此布局通常用于游戏或者处理一些画廊程序。如图:
<?xml version="1.0" encoding="utf-8"?>
- <!--帧布局,所以子控件均显示在屏幕的左上角,层叠式排列。此布局无法控制子控件的大小与位置,
- 但是子控件自身可以控制其位置大小-->
- <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/bg"
- >
- <!--图片显示控件并且在容器的右侧显示-->
- <ImageView
- android:id="@+id/one_imageview"
- android:src="@drawable/one"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="right"
- />
- <!--第二张图片显示在左侧底部-->
- <ImageView
- android:id="@+id/two_imageview"
- android:src="@drawable/two"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:scaleType="fitEnd"
- />
- </FrameLayout>
<!-- 帧布局,所以子控件均显示在屏幕的左上角,层叠式排列。此布局无法控制子控件的大小与位置, 但是子控件自身可以控制其位置大小 --> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg" > <!-- 图片显示控件 并且在容器的右侧显示 --> <ImageView android:id="@+id/one_imageview" android:src="@drawable/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" /> <!-- 第二张图片显示在左侧底部 --> <ImageView android:id="@+id/two_imageview" android:src="@drawable/two" android:layout_width="wrap_content" android:layout_height="fill_parent" android:scaleType="fitEnd" /> </FrameLayout>
package net.csdn.blog.androidtoast;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.ImageView;
- publicclassMainActivityextendsActivity{
- ImageViewmOneImageView;
- ImageViewmTwoImageView;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mOneImageView=(ImageView)findViewById(R.id.one_imageview);
- mTwoImageView=(ImageView)findViewById(R.id.two_imageview);
- //添加点击监听事件
- mOneImageView.setOnClickListener(newImageView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //点击one时隐藏自身显示two
- mTwoImageView.setVisibility(View.VISIBLE);
- v.setVisibility(View.GONE);
- }
- });
- mTwoImageView.setOnClickListener(newImageView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- mOneImageView.setVisibility(View.VISIBLE);
- v.setVisibility(View.GONE);
- }
- });
- }
- }
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { ImageView mOneImageView; ImageView mTwoImageView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mOneImageView=(ImageView) findViewById(R.id.one_imageview); mTwoImageView=(ImageView) findViewById(R.id.two_imageview); //添加点击监听事件 mOneImageView.setOnClickListener(new ImageView.OnClickListener(){ @Override public void onClick(View v) { //点击one时隐藏自身 显示two mTwoImageView.setVisibility(View.VISIBLE); v.setVisibility(View.GONE); } }); mTwoImageView.setOnClickListener(new ImageView.OnClickListener(){ @Override public void onClick(View v) { mOneImageView.setVisibility(View.VISIBLE); v.setVisibility(View.GONE); } }); } }
- LinearLayout.rar (552.6 KB)
- 下载次数: 359
- RelativeLayout.rar (244.1 KB)
- 下载次数: 294
- TableLayout.rar (39.9 KB)
- 下载次数: 286
- FrameLayout.rar (585.9 KB)
- 下载次数: 316
相关推荐
在博客“三,android编码规范 & 常用布局 & 常用控件”中,作者可能详细讲解了这些概念,并给出了实践中的示例和最佳实践。特别是文件名为"RelativeLayout"的子文件,可能深入探讨了相对布局的使用技巧,包括如何...
### Android编码规范详解 #### 一、概述 在Android开发中,遵循一套统一且严谨的编码规范至关重要。良好的编码规范不仅有助于提升代码的可读性和可维护性,还能促进团队协作,减少潜在的bug。本文将详细介绍Android...
### Android编码规范与常用布局详解 #### 一、Android编码规范 在开发Android应用时,遵循一定的编码规范有助于提升代码的可读性和可维护性。虽然Android官方并未明确指定一套编码规范,但在实际开发过程中,根据...
【Android编码规范】 Android开发中遵循一定的编码规范是非常重要的,因为它可以提高代码的可读性和可维护性。虽然Android官方没有提供正式的编码规范,但开发者社区已经形成了一些通用的约定。以下是一些主要的...
以下是一些关于Android编码规范的关键点: 1. **包命名规范**: - 包名遵循反域名规则,如`com.isa.crm.activity`,其中`com`代表顶级域名,`isa`可能是公司或个人名,`crm`是应用的领域,`activity`是模块名。 2...
《Android 编码规范》是指导开发者遵循统一标准,提高代码可读性和可维护性的重要文档。本规范基于《Java 编码规范》,并针对Android平台的特点进行了专门的规定。以下是对其中关键点的详细说明: 一、类及方法命名...
以下是对Android编码规范和命名规范的详细阐述: **编码规范** 1. **字符串资源管理**: - 应将所有可复用的文本内容放入`string.xml`文件中。这样做可以方便统一管理和修改,例如,如果需要将“保存”按钮的文本...
在Android开发中,编码规范是提高代码可读性、可维护性和团队协作效率的重要准则。虽然Android官方没有明确的编码规范,但开发者可以根据行业最佳实践和项目需求制定自己的规范。以下是一些基于Android源码和示例...
在Android开发中,自定义控件是提升应用独特性和用户体验的重要手段。本篇文章将深入探讨如何在Android中创建一个自定义的时钟控件,...在实际编程过程中,记得遵循Android的编码规范,保持代码的可读性和可维护性。
本文档旨在为开发者提供一套全面且实用的Android开发规范指南,帮助开发者养成良好的编码习惯。 #### 二、标识符命名法 1. **驼峰命名法**(Camel Case): - **小驼峰命名法**:用于变量和方法,首单词首字母...
### Android编码规范详解 本文档旨在提供一套详细的Android编码规范指南,帮助开发团队提升代码质量、增强可读性和可维护性。以下将详细介绍文件中提到的各项规范内容。 #### 1. 源文件基础 - **1.1 文件名**: ...
在Android开发中,遵循一套统一的编码规范至关重要,它不仅能够提高代码的可读性,也有利于团队协作和项目维护。TAC组为他们的安卓开发制定了一套详尽的编码规范,涵盖Java通用编码规范、前端独有编码规范以及TAC组...
#### 二、Android编码规范 除了命名规范外,编码规范也是提升代码质量的重要组成部分。虽然给定内容未具体提及,但通常包括但不限于: 1. **代码风格**:保持一致的缩进、空格使用等。 2. **注释**:对关键代码...
* 熟练 Android UI 的五种常用布局:线性、表格、相对、绝对、帧布局 * 熟练使用 textoiview、editview、button、menu、checkbox、listview 等界面组件 * 熟练 SqLite、File、SharePreference 和网络存储 * 熟练 ...
- **代码质量**:遵循良好的编码规范,提高代码可读性和可维护性。 #### 五、Android应用发布与市场推广 - **打包发布**:学习如何生成APK文件,并进行签名。 - **Google Play发布流程**:了解Google Play开发者...
6. **安全编程实践**:遵循安全编码规范,避免常见的安全漏洞,如SQL注入、跨站脚本攻击等。 通过阅读《Android高级开发实战 UI、NDK与安全》,开发者不仅可以提升Android应用的视觉效果和性能,还能确保其在面临...
在实际项目中,为了确保代码的可维护性和可读性,开发者通常会遵循良好的编码规范,如使用注释解释关键代码段,将复杂的逻辑拆分为多个方法,以及合理地组织类和文件结构。 由于这个压缩包中只有一个文件名,我们...