相对布局与文本系列的控件

####    RelativeLayout
相对布局

第一类相对:子视图相对于父容器,这类相对叫外相对,取值true/false

    android:layout_centerHorizontal 水平居中
    android:layout_centerVertical    垂直居中
    android:layout_centerInParent    居中(水平+垂直居中)
    android:layout_alignParentLeft\Right\Top\Bottom
第二类相对:子控件之间的相对,被参考的视图要有id,引用id方法"@id/id_name"

    android:layout_above    在谁的上面
    android:layout_below    在谁的下面
    android:layout_toLeftOf    在谁的左边
    android:layout_toRightOf    在谁的右边
    android:layout_alignLeft\Top\Right\Bottom跟谁左、顶部、右、底部对齐

####    布局技巧(复用性)
    <include>标签,可以导入已经写好的布局
    <include layout="@layout/title_layout" />

    <merge>可以对导入的布局直接过滤掉,让其子控件直接添加到父容器
    <merge xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    ...这其中的视图会直接引用他父容器的布局属性
    </merge>
    merge标签可以放在跟布局上,然后导入到其他布局,或者添加到系统布局中
####    文本系列的控件
文本框、按钮、输入框、单选按钮、多选按钮
#####    TextView
处理文本显示的控件

    android:text="@string/hello_world"    设置文本
    android:textColor="#ff0000"            文本颜色
    android:textSize="30sp"                文本大小(单位用sp)
    android:shadowColor="#ffff00"        阴影的颜色
    android:shadowRadius="3"            阴影的半径
    android:textScaleX="0.5"            文本拉伸和收缩
    android:textStyle="italic"    设置样式,bold粗体,italic斜体
    android:drawableLeft="@drawable/ic_launcher" 设置文本上
    的图标drawableLeft左侧图标,drawableTop顶部图标,
    drawableRight右侧图标,drawableBottom底部图标

    android:singleLine="true"表示单行显示
    android:lines="5" 限定显示行数
    android:maxLine="" 最大行数
    跑马灯设置
    android:ellipsize="marquee"    对过长文本的截取方式start在开头加"..." midle在中间加"..." 
    end在末尾加 marquee表示跑马灯
    android:marqueeRepeatLimit="marquee_forever" 设置跑马灯的重复
    次数 1 2 3 marquee_forever表示一直重复
跑马灯,要让文本可以运动,需要让文本框得到焦点,任何视图得到焦点有两种:
    
    第一种,在xml中使用
    android:focusable="true"
    android:focusableInTouchMode="true"
    
    第二种,在java中选中控件
    tv.setSelected(true);
#####    Button
继承自TextView

设置点击监听

    定义监听器类(在Activity中定义内部类)
    class MyClickListener implements OnClickListener{
        
        int count = 0;
        //设置监听器的控件被点击时触发
        @Override
        public void onClick(View v) {
            count++;
            tv.setText("按钮被点击了"+count);
        }
        
    }

    onCreate方法中设置监听器:
    Button btn = (Button) findViewById(R.id.m_btn);
    //设置点击监听的方法(需要监听器对象)
    btn.setOnClickListener(new MyClickListener());
#####    关于视图的显示和隐藏

    android:visiblity="visible" visible可见的 invisible不可见会占位置 gone不可见不占位置
    
    该属性在java中使用如下方式设置

    btnGone.setVisibility(View.VISIBLE); View.INVISIBLE  View.GONE


####    长按监听
需要实现OnLongClickListener接口

   

 private OnLongClickListener mLongClick = new OnLongClickListener() {
        
        @Override
        public boolean onLongClick(View v) {
            tvShow.setText("按钮被长按了");
            //false表示不拦截事件
            //true表示拦截事件
            return true;
        }
    };


注意onLongClick的返回值,false不拦截事件,true拦截事件

通过视图的setOnLongClickListener来设置监听

    btn1.setOnLongClickListener(mLongClick);
####    Logcat的使用
主要有5个等级的信息打印

    Log.v(tag,msg) 最全的详细日志
    Log.d(tab,msg) debug级别的打印
    Log.i(tab,msg)    info级别
    Log.w(tab,msg) warn级别
    Log.e(tab,msg) error级别的打印
如果需要导出机器中的日志可以使用adb指令

   
 adb logcat -v time>D:\log201704201032.txt


停止打印用Ctrl+C

####    EditText

    android:inputType="textPassword"设置输入类型,帮助输入法显示合适的键盘类型
    android:maxLength="5" 最大的宽度有多少个字符,宽必须是wrap_content
    android:ems="5"多少个字符的宽度
    android:hint="@string/et_hint"输入框提示
设置输入的光标位置

    et.setSelection(index); 设置输入位置.起始光标和结束光标位置一样

监听键盘的Enter键

       
et.setOnEditorActionListener(new OnEditorActionListener() {
            
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(v.getId() == R.id.et1){
                Log.e("m_tag","输入框输入完毕");
            }
            Log.e("m_tag",event.getKeyCode()+"===="+v.getText());
            //处理Enter按下之后的逻辑
            return false;
        }
    });



内容的处理

    Editable str = et.getText();
    str.append("2");
    str.insert(where, text)
    s.delete(start, end);
监听文本变化

    
// 监听输入的内容变化情况
    et.addTextChangedListener(new TextWatcher() {

            //改变中
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                Log.e("m_tag", "onTextChanged:" + s);
            }

            //改变前
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                Log.e("m_tag", "beforeTextChanged:" + s);
            }

            //改变后
            @Override
            public void afterTextChanged(Editable s) {
                Log.e("m_tag", "afterTextChanged:" + s);
                if (s.length() > 6) {
                    s.delete(6, s.length());
                }
            }
        });


####    单选按钮和多选按钮

    android:checked="true"设置默认选中状态 true为选中 false不选中
    android:button="按钮资源" 可以控制选择框前面的按钮 android:button="@null" 表示去掉前面的按钮

CheckBox

    xml中:
     
<CheckBox
        android:id="@+id/ch_moves"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="看电影" />


    java中可以监听选择变化
    /**
     * 选择框变化的监听器(单个选择框)
     */
   
 private CompoundButton.OnCheckedChangeListener onCkeckChange = new 
    CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                list.add(buttonView.getText().toString());
                setTitle(buttonView.getText() + "被选中");
            } else {
                list.remove(buttonView.getText().toString());
                setTitle(buttonView.getText() + "被取消选中");
            }
        }
    };


  
  设置监听
    ((CheckBox)findViewById(R.id.ch_moves)).setOnCheckedChangeListener(onCkeckChange);


RadioButton和RadioGroup,RadioGroup继承自LinearLayout

   
 <RadioGroup
        android:id="@+id/sex_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/r_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:id="@+id/r_woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>


在java中可以监听选择变化
    
   
 private RadioGroup.OnCheckedChangeListener onGroupCheckChange = new 
    RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton btn = (RadioButton) findViewById(checkedId);
            setTitle("选中了:"+btn.getText());
        }
    };
设置监听
    
    sexGroup = (RadioGroup) findViewById(R.id.sex_group);
    sexGroup.setOnCheckedChangeListener(onGroupCheckChange);
获取结果
    
    int id = sexGroup.getCheckedRadioButtonId();
清除所有的选中状态

    sexGroup.clearCheck();


####    StateListDrawable
需要在drawable下新建selector文件(如:check_button_drawable.xml)

    
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- 资源选择器,可以根据目标视图的状态来显示对应的资源(图片、颜色) -->
        <!-- 未选中 -->
        <item android:drawable="@drawable/nomal" android:state_checked="false"/>

        <!-- 选中状态 -->
        <item android:drawable="@drawable/checked" android:state_checked="true"/>

    </selector>


引用
    
    android:button="@drawable/check_button_drawable"

定义颜色做背景,需要定义在drawable下(其实背景使用颜色指的是ColorDrawable)

   
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:drawable="@color/bg_nomal" android:state_pressed="false" 
        android:state_enabled="true"></item>
        
        <item android:drawable="@color/bg_press" android:state_pressed="true" 
        android:state_enabled="true"></item>
        
        <!-- 不可用 -->
        <item android:state_enabled="false">
            <color android:color="#ff666666"/>
        </item>

    </selector>


如果要定义文本颜色(颜色值)的选择器(int类型的颜色,不是drawable),需要在res下新建color文件夹,然后在color文件夹下新建selector的xml文件(如:tc_color.xml)

   
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:state_pressed="false" android:color="#ff000000"/>
        
        <item android:state_pressed="true" android:color="#ffffffff"/>

    </selector>


使用

    
<Button
        android:layout_width="200dip"
        android:layout_height="50dip"
        android:background="@drawable/btn_color_bg"
        android:text="Click Me"
        android:textColor="@color/tx_color" />



####    shape标签

    
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 填充颜色、渐变色 -->
    <gradient
        android:angle="45"
        android:centerColor="#ffffff"
        android:endColor="#ff0000"
        android:gradientRadius="50"
        android:startColor="#ffff00"
        android:type="radial" />

    圆角
    <corners
        android:bottomRightRadius="15dp"
        android:topLeftRadius="15dp" />

    内边距
    <padding
        android:bottom="15dp"
        android:left="10dp"
        android:right="10dp"
        android:top="15dp" />

    实色填充
     <solid android:color="#ff0000" />

    边框
    <stroke android:dashWidth="2dip" 虚线宽度
     android:dashGap="3dp" 虚线的间隔
     android:width="2dip" 边框宽度
     android:color="#ffff00" /> 

    </shape>



    

    

    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值