布局
LinearLayout
常用属性
android:id
相当于姓名android:layout_width
宽度android:layout_height
高度Android:text
文字,如果是英文默认全大写android:textAllCaps=“false”
可以关闭全大写android:background
背景android:layout_margin
外边距android:layout_padding
内边距android:orientation
布局方向(横竖),默认为horizontal水平排列android:gravity
容器内部元素的排列方式,如居中靠下,靠上等android:layout_weight
该容器占父容器剩余空间的权重值wrap_content
适应内容,内容有多少,宽度就为多少match_parent
适应父容器,上一级宽度是多少,这一级就是多少- 在安卓里长度单位通常使用dp,字体使用sp,因为Andriod手机的屏幕不一样,使用像素px会导致不同的屏幕效果不一样,dp可以自动适配
RelativeLayout
常用属性
android:layout_toLeftOf
在谁的左边,值为idandroid:layout_toRightOf
在谁的右边,值为idandroid:layout_alignBottom
跟谁底部对齐android:layout_alignParentBottom
跟父容器底部对齐,值为true或falseandroid:layout_alignParentRight
跟父容器右边对齐,值为true或falseandroid:layout_below
在谁的下面,值为id
TextView
-
文字大小单位sp、颜色
-
显示不下使用…表示
- 方法一
android:maxLines=“1”
最多显示一行,显示不全的就舍弃
android:ellipsize=“end”
加上这一行,显示不出来的用…表示 - 方法二
android:singleLine=“true”
- 方法一
-
文字+icon(下拉列表)
android:text=“下拉列表”
android:drawableRight="@drawable/icon_arrow_off" 箭头图片
android:drawablePadding=“5dp” 图片和文字之间的间隔
-
中划线,下划线
- 在Activity里获取到TextView的方法
private TextView mTv4; //声明
mTv4 = findViewById(R.id.tv_4); //获取
- 在Activity里通过findViewById()获取TextView后添加以下代码
mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //给mTv4添加中划线,有的设备可能会有锯齿 mTv4.getPaint().setAntiAlias(true); //去除锯齿 mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //给mTv5添加下划线
- 在.xml文件中不设置文字android:text="",在Activity里通过findViewById()获取TextView后添加以下代码
mTv6.setText(Html.fromHtml(“HTML代码”)); //使用下面的这段HTML添加文字和下划线
<u>我的第一个App</u>
- 在Activity里获取到TextView的方法
-
跑马灯效果
在.xml文件的TextView里添加以下代码,注意确保android:text足够长,无法在一行显示完,这样才能跑起来android:singleLine="true" 单行显示 android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" 控制时间,这里是永远跑下去 android:focusable="true" android:focusableInTouchMode="true"
Button(TextView的子类)
- 字体大小、颜色
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:background="#FF0000"/>
-
自定义背景形状
- 在drawable文件夹下新建.xml文件,下面展示的是自定义圆角按钮(.xml文件Root element为shape)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF9900"/> <corners android:radius="5dp"/> </shape>
android:background="@drawable/bg_btn2"
引用自定义的背景bg_btn2- 自定义边框按钮,把上面的
<solid android:color="#FF9900"/>
,换为<stroke android:width="1dp" android:color="#FF9900"/>
- 在drawable文件夹下新建.xml文件,下面展示的是自定义圆角按钮(.xml文件Root element为shape)
-
自定义按压效果