3、基本控件


        控件,很好的概念,不多说。安卓自带(包含、集成)的控件很多,说几个常用的:TextView(文本显示,上一节的你好,世界),Button(按钮,不多说),ImageView(图片,显示一张图片、图标),EditText(编辑框,可弹出输入法输入文字),LinerLayout(线性布局),RelativeLayout(相对布局)。布局也可以说是控件,通常包含一些子类控件,管理子类的位置关系。


安卓控件一般包含:

1、一些属性(xml中):
        (1)android:id(代码中索引控件的一个标志),@+id/tv_review,代码中可以使用R.id.tv_review(是一个整形值,开发工具会自动生成)。
        (2)android:layout_width(宽),xdp(固定多少dp),wrap_content(包含内容),match_parent(充满父类)。
        (3)android:layout_height(高),xdp(固定多少dp),wrap_content(包含内容),match_parent(充满父类)。
        (4)android:padding(自身内容到自身边界的距离),xdp(固定多少dp)。
        (5)android:layout_margin(自身边界到父类(一般是布局)的距离),xdp(固定多少dp)。

上述是大多安卓控件都有的属性,不同控件也有其自身特有的属性。


2、一些方法(什么?):
        很多,如:getText(),setText(),下面会用到,再说。
3、一些事件(什么?):
        如:点击(onClick)等。


一、TextView
        显示一行(一段)文本,如你好,世界。其属性android:text是要显示的文本,android:textColor为字体颜色,android:textSize字体大小等,属性很多可慢慢掌握。

二、Button
        按钮,就是按钮。其属性android:text是按钮上的文字,android:textColor为字体颜色,android:textSize字体大小等。

三、ImageView
        显示图片的容器。其属性android:src为要显示的图片等。

四、EditText
        编辑框(输入框),接受用户输入法输入。android:text为输入的内容等。

五、LinerLayout
        线性布局,android:orientation设置方向,vertical纵向,其中包含的子类纵向向下排列,horizontal横向,其中包含的子类横向向右排列。

六、RelativeLayout
        相对布局,其中的子类可以指定相对的位置关系,android:layout_alignParentLeft,值设为true时,子类位于父类左侧,同理android:layout_alignParentRight,位于父类右侧。等,慢慢了解。

        用上面的最基本的控件和布局,搭一个用户回复的小页面:


        最上方一个TextView,显示用户的评论,下方一个EditView,用户输入,再下方,左侧一个表情的图标(ImageView),右侧回复按钮(Button)。当用户点击回复按钮(Button)时,将输入的内容(EditText)显示到上方的TextView中。

一、界面

        分析,共包含三块区域,成纵向排列,可以用LinerLayout,最下方一个靠左,一个靠右,可以用RelativeLayout(作为子类包含在LinerLayout)。修改xml如下:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f3f3f3" >

    <LinearLayout
	  android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="6dp"
          android:orientation="vertical"
          android:background="#ffffff">
        
    	  <TextView
                android:id="@+id/tv_review"
        	android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:layout_margin="12dp"
        	android:text="暂无回复"
        	android:textSize="14sp" />
    	  <View
	  	android:layout_width="match_parent"
        	android:layout_height="6dp"
        	android:background="#f3f3f3" />
    	  <EditText
                android:id="@+id/et_review"
	  	android:layout_width="match_parent"
        	android:layout_height="60dp"
        	android:layout_marginTop="12dp"
        	android:layout_marginLeft="12dp"
        	android:layout_marginRight="12dp"
        	android:textSize="14sp"
        	android:background="#e0e0e0"/>
    	  <RelativeLayout
        	android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:layout_marginLeft="12dp"
        	android:layout_marginRight="12dp"
        	android:layout_marginTop="6dp"
        	android:layout_marginBottom="12dp">
        
        	<ImageView 
                    android:layout_width="30dp"
        	    android:layout_height="30dp"
           	    android:src="@drawable/ic_launcher"/>
        	<Button
                    android:id="@+id/bt_review"
		    android:layout_width="70dp"
        	    android:layout_height="30dp"
       	            android:layout_alignParentRight="true"
        	    android:text="回复"
        	    android:textSize="14sp"
        	    android:background="#0088ff"/>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

二、功能

下节实现。


坚持不懈——2016/10/19


猜你喜欢

转载自zdphpn.iteye.com/blog/2367127