Five-People:常用控件及其属性(RadioButton,CheckBox,align,margin,padding)使用方法

来源:互联网 发布:联通网络机顶盒破解 编辑:程序博客网 时间:2024/06/06 07:12

     1.CheckBox  (多选框):

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/trip"
            android:text="@string/trip"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/literature"
            android:text="@string/literature"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/design"
            android:text="@string/design"/></LinearLayout>
</LinearLayout>

效果图:


2.RadioButton:一组RadioButton必须要放到一个RadioGroup中;

        RadioButton只能实现单选;

        要给每个RadioButton加ID,否则点击再另一个则显示双选状态

        checked赋给RadioButton点击状态

演示:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <RadioGroup        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical">        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/male"            android:text="男"            android:checked="true"/>        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/female"            android:text="女"/>    </RadioGroup></LinearLayout>
效果图:


3.align (RelativeLayout); 

                android:layout_alignParentBottom 贴紧父元素的下边缘
      android:layout_alignParentLeft 贴紧父元素的左边缘
      android:layout_alignParentRight 贴紧父元素的右边缘
      android:layout_alignParentTop 贴紧父元素的上边缘
      android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

            android:layout_alignParentStart紧贴父元素结束位置开始

            android:layout_alignParentEnd紧贴父元素结束位置结束

代码示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp">
        <EditText
            android:layout_width="200dp"
            android:layout_height="60dp"
            android:id="@+id/et_item"
            android:layout_marginLeft="40dp"
            android:background="@drawable/et_item"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/iv_item"
            android:background="@mipmap/select"
            android:layout_alignRight="@+id/et_item"
            android:layout_marginTop="8dp"
            android:layout_marginRight="10dp"
            android:paddingRight="20dp"
            android:paddingBottom="20dp"/>
    </RelativeLayout>
</LinearLayout>

效果图:


4.margin;padding 

Margin与padding的区别:
android:paddingLeft="30px"  按钮上设置的内容离按钮左边边界30个像素
android:layout_marginLeft="30px"  整个按钮离左边设置的内容30个像素

Padding:站在父View的角度描述问题,它规定里面的内容比喻与这个父View边距的距离
Margin:则是站在自己的角度描述问题,规定自己和其他(上下左右)的VIew之间的距离,如果同一级只有一个VIew,那么他的效果基本上和Padding一样

margin:设置所有外边距属性,当前对象距父对象的四边距,边距值可以为距离值

android:layout_margin 本元素离上下左右间的距离
   android:layout_marginBottom 离某元素底边缘的距离
   android:layout_marginLeft 离某元素左边缘的距离
   android:layout_marginRight 离某元素右边缘的距离
   android:layout_marginTop 离某元素上边缘的距离
android:layout_marginStart:本元素里开始的位置的距离
android:layout_marginEnd本元素里结束位置的距离

padding;

属性定义元素边框与元素内容之间的空白区域;不允许负值。
android:paddingStart指定布局左边与子布局的间距与android:paddingLeft相同
android:paddingEnd指定布局右边与子布局的间距与android:paddingRight相同
 android:padding指定布局与子布局的间距
        android:paddingLeft指定布局左边与子布局的间距
        android:paddingTop指定布局上边与子布局的间距
        android:paddingRight指定布局右边与子布局的间距
        android:paddingBottom指定布局下边与子布局的间距
padding 效果图:
android:paddingLeft="50dp"android:paddingRight="50dp"android:paddingTop="30dp"android:padding="30dp"
(距离上下左右的边距)

(距离左边距50dp)

margin的效果图:

android:layout_marginBottom="40dp"(rght left top同)

android:layout_marginRight="30dp"(在btn1中)
btn1与btn2之间相距30dp

android:layout_margin="50dp"



5.gravity;

属性是对该view中内容的限定.比如一个button 上面的text. 你可以设置该text 相对于view的靠左,靠右等位置.
演示:
android:gravity="bottom"(right,left,top同)android:gravity="center_horizontal" (水平居中



6.layout_gravity:

用来设置该view相对与父view 的位置.比如一个button 在linearlayout里,你想把该button放在linearlayout里靠左、 靠右等位置就可以通过该属性设置. 

注意:对于LinearLayout如果设置android:orientation="vertical",那么android:layout_gravity的设置只在水平方向生效,
如果设置android:orientation="horizontal",那么android:layout_gravity属性只在垂直方向生效
演示:
android:layout_gravity="center"(默认竖直)android:layout_gravity="bottom"

7.控件背景与边框的设置(drawable中定义)
(1)定义背景图(通过background间接引用drawable中的图片;实现时间被点击与未被点击时的不同背景)
演示:


<LinearLayout    android:layout_width="match_parent"    android:layout_height="0dp"    android:layout_weight="1"    >    <RadioGroup        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal"        android:layout_marginLeft="15dp"        android:layout_marginRight="15dp">        <RadioButton            android:layout_width="0dp"            android:layout_weight="1"            android:background="@drawable/rbtn_selecter"            android:layout_height="wrap_content"            android:text="学员"            android:id="@+id/rbtn_stu"            android:checked="true"           android:layout_gravity="center_vertical"/>        <RadioButton            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="教练"            android:id="@+id/rbtn_teach"            android:background="@drawable/rbtn_selecter"            android:layout_gravity="center_vertical"/>        <RadioButton            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="陪练"            android:id="@+id/rbtn_partner"            android:background="@drawable/rbtn_selecter"            android:layout_gravity="center_vertical"/>        <RadioButton            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="校长"            android:id="@+id/rbtn_president"            android:layout_gravity="center_vertical"            android:background="@drawable/rbtn_selecter"            />    </RadioGroup></LinearLayout>
效果图:

(2)设置控件的边框(root element:shape)

Shape:<solid:背景颜色>

        < Stroke:对边框的颜色,宽度的设置>

<corners:弧度角,radius=“2px”>

在drawable中:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#00ffff"></solid>    <stroke android:color="#000000" android:width="10px"></stroke></shape>
应用到控件中:

<EditText    android:layout_width="200dp"    android:layout_height="60dp"    android:id="@+id/et_item"    android:layout_marginLeft="40dp"    android:background="@drawable/et_item"    />
效果图:



0 0