1.Android开发-UI布局
1.1线性布局
1.1.1线性布局的使用
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
1.1.2线性布局摆放的方向
我们可以通过orientation这个属性修改LinearLayout布局的孩子摆放方向,它的值有两个:
vertical
horizontal
1.1.3线性布局中的权重
当有些时候,我们需要平均给孩子宽度或高度,可以用权重
//有时候不平均,但是点的宽/高成比例,我们可以用权重解决
//平均,每个权重为1,即每个占1/n
1.2相对布局
1.2.1相对布局相对于父控件
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中间" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="右上角" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="右下角"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="左上角"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="左下角"/>
</RelativeLayout>
效果图
1.2.2相对布局相对于同级控件
这是相对于同级控件的摆放
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--这个是中间-->
<Button
android:id="@+id/center_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="确定"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/center_button"
android:text="<-"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@id/center_button"
android:text="我在中间的顶部"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/center_button"
android:text="->"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/center_button"
android:text="我在中间的底部"/>
</RelativeLayout>
效果图
1.3其他布局
1.3.1绝对布局AbsoluteLayout
AbsoluteLayout是靠xy来控制自己的位置的
机顶盒、手表;分辨率是固定的
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="125dp"
android:layout_y="131dp"
android:text="Button" />
</AbsoluteLayout>
效果显示
1.3.2表格布局TableLayout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"/>
</TableRow>
</TableLayout>
效果显示
1.3.3帧布局FrameLayout
使用场合:播放器
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#ff00"/>
</FrameLayout>
效果显示
1.4布局中常用的单位
1.4.1像素单位px
不建议使用,除非是手表,或者机顶盒
1.4.2适配的单位dp (Density Independent Pixels)
推荐使用,在实际开发中,UI设计师会给你标好
http://blog.sunofbeaches.com/archives/196
也就是独立密度的意思,与像素没有关系,它的标准是:以160dip为基准,在这个dip的条件下:1dp = 1px;其他屏幕大小进行比例变化即可!这个单位是屏幕适配的哦!为什么呢?请看下面的文字理解一下吧:
假设标准的屏幕是160dpi,小米2s的屏幕dpi = 342、然后的话,假设在标准屏幕的图片宽度为1dip.也就是1像素。也就是说,在标准屏幕上显示1dip的大小为1像素。而在小米2s上像是是多少呢?我们掐指一算:得出 342/160 = 2.1375.也就是在小米2s上1dip表示的大小为2.375像素。
1.4.3字体单位sp(scaled pixels-best for text size)
放大像素(比例像素),与刻度无关,可以根据用户的字体大小首选项进行缩放,主要用来处理字体的大小;
阳光沙滩课程资料下载 - bbs.sunofbeaches.com http://bbs.sunofbeaches.com/forum-216-1.html
1.5练习
1.5.1计算器
1.5.3计算器的点击特性实现
1.5.4实现控件的点击事件
第一种:在要被点击的控件里添加onClick属性
格式:
android:onClick=”方法名”
android:onClick="oneOnClick"
接下来,我们就在对应使用这个布局的Activity上面写一个方法,这个方法的格式为:
public void 方法名(View view)
{
方法内容;
}
比如说,我们上面的这个oneOnClick这个点击事件处理如下:
public void oneOnClick(View view) {
//被点击以后的执行语句;
Log.d(TAG,"one be click");
}
开发案例:
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_white_bg"
android:gravity="center"
android:onClick="oneOnClick"
android:text="C"
android:textSize="30sp" />
//下面是点击事件的处理:
public void oneOnClick(View view) {
Log.d(TAG,"one be click");
if (view instanceof TextView) {
String oneText = ((TextView) view).getText().toString();
Log.d(TAG,"one btn content == " + oneText);
}
}
第二种方式:
通过id声明的方式来找到控件,再对这个控件设置点击事件:
步骤:
第一步:给对应的控件添加id号
<TextView
android:id="@+id/tv_number_zero"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center_vertical"
android:background="@drawable/selector_white_bg"
android:paddingLeft="40dp"
android:text="0"
android:textSize="30sp"/>
<TextView
第二步:在对应的Activity里找到控件
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private TextView mCancel;
private TextView mPlusOrMinud;
private TextView mMod;
private TextView mDivider;
private TextView mOne;
private TextView mTwo;
private TextView mThree;
private TextView mTime;
private TextView mFour;
private TextView mFive;
private TextView mSix;
private TextView mMinus;
private TextView mSeven;
private TextView mEight;
private TextView mNine;
private TextView mPlus;
private TextView mZero;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caculator_layout);
//找控件
initView();
}
/*
*在这里找到控件
*/
private void initView()
{
mCancel = (TextView) this.findViewById(R.id.tv_cancel);
mPlusOrMinud = (TextView) this.findViewById(R.id.tv_plus_or_minus);
mMod = (TextView) this.findViewById(R.id.tv_mod);
mDivider = (TextView) this.findViewById(R.id.tv_divider);
mOne = (TextView) this.findViewById(R.id.tv_number_one);
mTwo = (TextView) this.findViewById(R.id.tv_number_two);
mThree = (TextView) this.findViewById(R.id.tv_number_three);
mTime = (TextView) this.findViewById(R.id.tv_time);
mFour = (TextView) this.findViewById(R.id.tv_number_four);
mFive = (TextView) this.findViewById(R.id.tv_number_five);
mSix = (TextView) this.findViewById(R.id.tv_number_six);
mMinus = (TextView) this.findViewById(R.id.tv_minus);
mSeven = (TextView) this.findViewById(R.id.tv_number_seven);
mEight = (TextView) this.findViewById(R.id.tv_number_eight);
mNine = (TextView) this.findViewById(R.id.tv_number_nine);
mPlus = (TextView) this.findViewById(R.id.tv_plus);
mZero = (TextView) this.findViewById(R.id.tv_number_zero);
}
}
第三步:设置点击事件
/*
*设置点击事件
*/
private void initClickEvent(){
//第一种设置方式
mCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Log.d(TAG, "content=="+((TextView)v).getText() );
}
});
//第二种设置方式
mPlusOrMinus.setOnClickListener(this);
mPlus.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//如果有多个空间设置点击事件,我们这里需要统一处理的话,需要判断是
//哪一个控件
if(v == mPlusOrMinus){
Log.d(TAG, "点击了mPlusOrMinus" + ((TextView)v).getText().toString());
}else if(v == mPlus){
//同样的方式去判断
}
//另一种方式及时用switch来判断id
//先拿到id
int id = v.getId();
switch(id){
case R.id.tv_number_one:
//one这个内容被点击了,就在这里处理
break;
case R.id.tv_equal:
//处理等号事件
break;
}
}
}
1.5.5logcat的使用和设置
我们在平时开发时,需要打出一些运行时的信息或者标记,用于测试或者说用于修改bug,以获取我们应用运行时的信息。
Logcat有六个级别:
- Verbose: 会输出所有的信息
- Debug: 会输出调试信息
- Info: 是级别为Info的信息
- Warn: 这个是警告级别的信息
- Error: 是错误的信息
- Assert: 是断点信息
因为log非常多,所以有时候需要进行过滤
配置logcat的颜色
1.6 学习网站:
http://blog.sunofbeaches.com/archives/category/Android/page/2