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