android:layout_gravity=”center_horizontal” 写到布局文件的控件中, 可以使控件居中
一般控件的大小单位用 dp 表示
android:layout_width=”match_parent” 控件宽度填满父控件
android:gravity=”center” 控件内部水平居中
android:textSize=”24sp” 字体大小一般用 sp 做单位
android:hint=”Type something here” EditText 控件默认提示的文字
android:maxLines=”2” EditText 控件最大显示文字行数
imageView.setImageResource(R.drawable.jelly_bean); ImageView 设置图片资源
所有的Android控件都具有这个属性,可以通过android:visibility进行指定,可选值有三种,visible、invisible和gone。
visible表示控件是可见的,这个值是默认值
invisible表示控件不可见,但是它仍然占据着原来的位置和大小,可以理解成控件变成透明状态了
gone则表示控件不仅不可见,而且不再占用任何屏幕空间
我们还可以通过代码来设置控件的可见性,使用的是setVisibility()方法,可以传入View.VISIBLE、View.INVISIBLE和View.GONE三种值
我们还可以给ProgressBar指定不同的样式
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
/>
指定成水平进度条后,我们还可以通过android:max属性给进度条设置一个最大值
int progress = progressBar.getProgress();
progress = progress + 10;
progressBar.setProgress(progress)
用代码来改变 进度条的值吧
AlertDialog可以在当前的界面弹出一个对话框
AlertDialog.Builder dialog = new AlertDialog.Builder (MyActivity.this);
dialog.setTitle("This is Dialog");
dialog.setMessage("Something important.");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", new DialogInterface. OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton("Cancel", new DialogInterface. OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
ProgressDialog会在对话框中显示一个进度条,一般是用于表示当前操作比较耗时
android:layout_weight=”1” 在布局中表示权重值 默认值为 : 0
RelativeLayout又称作相对布局,也是一种非常常用的布局
基于父控件的相对布局
android:layout_alignParentRight=”true” 以父控件 右对齐
android:layout_alignParentLeft=”true” 以父控件 左对齐
android:layout_alignParentTop=”true” 以父控件 顶对齐
android:layout_alignParentBottom=”true” 以父控件 底对齐
android:layout_centerInParent=”true” 以父控件 水平垂直居中
基于控件的相对布局
android:layout_above=”@id/button3” 表示 该控件在 button3 这个控件的 上方
android:layout_toLeftOf=”@id/button3” 表示 该控件在 button3 这个控件的 左方
android:layout_toRightOf=”@id/button3” 表示该控件在 button3 这个控件的 右方
android:layout_below=”@id/button3” 表示该控件在 button3 这个控件的 下方
FrameLayout 这种布局没有任何的定位方式,所有的控件都会摆放在布局的左上角 (先写的控件 会放到下面, 后写的控件 会放到上面)
TableLayout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow
android:gravity="center"
android:layout_width="match_parent"
>
<TextView
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your account" />
</TableRow>
<TableRow
android:gravity="center"
>
<TextView
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow
android:gravity="center"
>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login" />
</TableRow>
</TableLayout>
在TableLayout中每加入一个TableRow就表示在表格中添加了一行
然后在TableRow中每加入一个控件,就表示在该行中加入了一列,TableRow中的控件是不能指定宽度的。
android:inputType=”textPassword” 指定 EditText 控件为密码框
android:layout_span=”2” 让登录按钮占据两列的空间
TableLayout 的 android:stretchColumns 属性 , 设置指定列数 允许拉伸
Android Studio
Eclipse中的WorkSpace相当于AS中的Project;
Eclipse中的Project相当于AS中的Module(只就新建而言)。
所以Eclipse中在一个WorkSpace中建立多个Project 等同于 在AS的Project中建立多个Module。
这篇文章, 会不定时更新, 做个笔记吧, 嘻嘻