android中常用的基本控件

Android自定义控件销毁方法_android

1.TextView

TextView属性

<1>设置字体大小推荐使用sp作为单位

设置宽度或高度等属性时推荐使用dp(dip)作为单位

android:TextSize=”20sp”

<2>设置超链

android:autoLink设置是否为文本URL链接/email/电话号码/map时,文本显示为可点击的链接

android:autoLink=”phone”

<3>、设置字体颜色

android:textColor=”#00FF00”
<4>、富文本
改变局部字体的颜色:

mTextImage = (TextView) findViewById(.textview_image);
    mTextImage.setText( Html.fromHtml("我是一个<font color='#ff0000'>富文本</font>,然后<img src = 'ic_launcher '/> 中间加一个图标"));

Android自定义控件销毁方法_控件_02

运用:

1.利用TextView和EditText,做一个用户登陆的界面
<LinearLayout xmlns:android="http:///apk/res/android"
    android:orientation="vertical"                                   android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是白云"
        android:id="@+id/textview_image"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">//布局是横向分布
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名"/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入手机号"
            android:drawableLeft="@mipmap/ic_launcher"//图标
            android:inputType="phone"//设置用户名是手机号
            android:id="@+id/name"/>
    </LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:password="true"//设置此文本是password类型的,密码不可见
        android:id="@+id/password"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查看密码"
        android:id="@+id/see_password" />
</LinearLayout>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入身份证号"/>
    <Button//添加一个登陆按钮
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:id="@+id/button_press"
        android:background="@drawable/button_press"
        />
</LinearLayout>

运行界面如下:

Android自定义控件销毁方法_Text_03

2、手机主界面下方四个图标:

Android自定义控件销毁方法_android_04


代码如下

<RelativeLayout xmlns:android="http:///apk/res/android"
    xmlns:tools="http:///tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mTextView"
        android:text="我要打电话17091158258"
        android:textColor="@color/red"
        android:textSize="20sp"
        android:autoLink="all"
        android:padding="30dp" 
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">
       <TextView
           android:layout_width="0dip"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:drawableTop="@mipmap/ic_launcher"
           android:gravity="center_horizontal"/>
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:gravity="center_horizontal"/>
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:gravity="center_horizontal"/>
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:gravity="center_horizontal"/>
    </LinearLayout>
</RelativeLayout>
3、TextView跑马灯
<TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/paomadeng"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="寒蝉凄切,对长亭晚,骤雨初歇。都门帐饮无绪,留恋处,兰舟摧发。执手相看泪眼,竟无语凝噎。念去去千里烟波,暮霭沈沈楚天阔。
多情自古伤离别,更那堪冷落清秋节。今宵酒醒何处,杨柳岸、晓风残月。此去经年,应是良辰好景虚设。便纵有千种风情,更与何人说。"

        />

解释:
android:ellipsize=”start” 省略号在开头
android:ellipsize=”middle” 省略号在中间
android:ellipsize=”end” 省略号在结尾
android:ellipsize=”marquee” 跑马灯显示
或者在程序中可通过setEillpsize显式设置。
android:focusable=”true” //要显示该跑马灯,view必须要获得焦点,只有在取得焦点的情况下跑马灯才会显示。

结果如下:

Android自定义控件销毁方法_控件_05

2、Button

<1>基本用法

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:id="@+id/button_press"
        android:background="@drawable/button_press"//填充图片
        />

//在主函数中创建点击事件
     mbt = (Button) findViewById(.button_press);
      mbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               mbt.setTransformationMethod(null);
            }
        });

<2>RadioButton
用于选择
如下:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="选择性别"
    android:textSize="30dp"/>
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radiogroup"
        android:checkedButton="@id/radio"//默认是男的id
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radio"
            android:text="男"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其它"/>
    </RadioGroup>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_select"
        android:text="确定"/>
</LinearLayout>

Android自定义控件销毁方法_xml_06

综合运用

大美农登陆界面

分析:整体为线性布局 vertical上下分布

ImageText >>LinearLayout>>登陆Button>>相对布局 RelativeLayout

界面如下:

Android自定义控件销毁方法_xml_07

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:///apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00cc33"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@mipmap/a_logo1"
        android:layout_marginTop="35dp"/>

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="@drawable/input"
        android:orientation="vertical"
        android:layout_marginTop="30dp">

        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@mipmap/login_icon01"
            android:hint="请输入手机号"
            android:inputType="phone" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@mipmap/login_icon02"
            android:hint="请输入密码"
            android:password="true" />
    </LinearLayout>

    <Button
        android:id="@+id/button_press"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button_press"
        android:text="登陆"
        android:textColor="#00cc33"
     android:layout_marginTop="25dp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="立即注册"
            android:padding="10dp"
            android:textColor="#ffffff"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:gravity="center_horizontal"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="忘记密码"
            android:padding="10dp"
            android:textColor="#ffffff"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:gravity="center_horizontal"/>
    </RelativeLayout>


</LinearLayout>