1.要开发“用户名___________”这种格式的登录界面的话,用表格布局,并且在TableRow中使用TextView与EditView配合,但是需要注意的是EditView的layout_width属性的值只能设置为一个具体的数值,比如300px,而不能设置为wrap_content。
2.还要注意,对于TextView和EditvVew,layout_width和layout_height属性是设置组件的基本宽度和高度;而android:width用于指定文本的宽度,以像素为单位,而android:height用于指定文本的高度,以像素为单位。edit view是textview的子类。
3.android:layout_width,android:layout_height,android:id,android:background是所有android.view.View 和 android.view.ViewGroup都支持的属性。
4.为view添加单事件监听器的方法有两种:一种是普通的用Java代码完成,一般写在Activity的onCreate()函数中,代码格式如下:
import android.view.view.onclicklistener
Button login = (Button)findViewById(R.id.login);
login.setOnClickListener(new OnClickListener(){
@override
public void onClick(View v){
//编写要执行的动作代码
}
});
另一种是在Activity中编写一个包含View类型参数的方法,并且将要触发的动作代码放在该方法中,然后在布局文件中,通过anroid:onClick属性指定对应的方法名实现。例如,在activity中编写一个名为myClick()的方法,关键代码如下:public void myClick(View view)
{//编写要执行的动作代码
}
4.layout_weight 设置
如果在一个LinearLayout里面放置两个Button,Button1和Button2,Button1的layout_weight设置为1,Button2的layout_weight设置为2,且两个Button的layout_width都设置为fill_parent。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"/>
</LinearLayout>
则Button1占据屏幕宽度的三分之二,而Button2占据三分之一,如下图所示:
如果两个Button的layout_width都设置成wrap_content,则情况刚好相反。Button1占三分之一,Button2占三分之二,如下图所示:
layout_weight在使用LinearLayout设计复杂的布局时还是挺有用处的,例如,在水平的线性布局中,你要分足够的空间给控件1,剩下的空间则分配给控件2,则只要设置控件1的layout_width设置为wrap_content,不用设置layout_weight,而在控件2中,设置layout_width为fill_parent,或layout_weight为1即可实现。