安卓UI学习笔记

layout布局中的组件

通用

  • android:layout_width=”wrap_content”
  • android:layout_height=”wrap_content”
  • android:text=”Hello”
  • android:textSize=”25dp”
  • android:textColor=”@color/red”字体颜色
    • #FF0000
    • 也可以直接android:background=”#FF1111”
    • 也可以使用android库里的颜色
    • android:background=”@android:color/holo_red_dark”背景色
  • android:id=”@+id/textView”
  • android:singleLine=”true”//使文本框在一行显示,已经被舍弃
  • 现在为android:lines=”1”或android:maxLines=”1”
  • android:gravity=”left”设置文字的对齐方式,可以用如下形式
    • android:gravity=”center_horizontal|clip_vertical”不能冲突
  • layout_margin=“10dp”设置边距

按钮

  • android:onClick=”getScore”可以实现在点击时调用getScore这个方法
  • android:textAllCaps=”false”使按钮显示的内容中英文不要默认为大写

输入框

  • android:inputType=”textPassword”密文显示内容
  • android:hint=”please input number”输入框的提示内容
  • android:maxLines=”2”上面已经说过,使输入框的最大行数为2,如果还在继续输入,文本向上滚动,而输入框不再拉伸
  • android:inputType=”number”设置输入的内容类型

图片

  • android:src=”@mipmap/ic_launcher”设置图片的显示内容

进度条

  • ProgressBar组件名
  • style=”?android:attr/progressBarStyleHorizontal”设置进度条的样式,默认为圆形,这种为长条形
  • android:max=”100”进度条的最大值,自行设定
  • android:progress=”20”当前进度值

对话框

  • AlertDialog组件名
  • 这种组件置顶于所有的界面元素之上,能够屏蔽掉其他控件的交互能力
  • 在activity之类的java代码中新建生成对话框,不能再layout中设置

对话框的代码:

AlertDialog.Builder dialog = new AlertDialog.Builder(SecondActivity.this);
dialog.setTitle("This is title");
dialog.setMessage("this is message");
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.setNeutralButton("3333", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        ……
    }
});
dialog.show();

其中,代码中设置生成的点击布局为3333 cancel ok ,其中dialog.setCancelable(false)设置的是对话框是否可以取消,如果设置为true,可以点击一下对话框的其他区域取消对话框的显示。

ProgressDialog等待框体

类似对话框,但是显示的是一个进度条,一般用在当前操作比较耗时,请用户等待流程中。

设置组件的可见性

通过以下代码设置

private Button bt = (Button) findViewById(R.id.button_4);
public void click(View v) {
    switch (v.getId()) {
        case R.id.button_1:
            if(bt.getVisibility() == View.GONE) {
                bt.setVisibility(View.VISIBLE);
            } else {
                bt.setVisibility(View.GONE);
            }
            break;
        default:
    }
}

其中getVisibility是获得组件的Visibility属性,每个组件都拥有这个属性,在不进行设定时默认为View.VISIBLE,即可见的,可以设置为View.GONE,为不可见,并且不再占据屏幕空间,也可以设置为View.INVISIBLE,表示不可见但是占据屏幕空间

布局样式

线性布局LinearLayout

  • gravity与layout_gravity的区别
    • android:layout_gravity=”center”
    • android:gravity=”center”
    • 上面这两种前者为该控件在页面布局中的属性,后者为文本内容在控件中的属性
  • vertical与horizontal的区别
    • horizontal设置的布局中,各个组件设置对应的位置属性,使用android:layout_gravity来设置,只能确定的是竖直上的位置,而水平位置不能设定
    • vertical设置的布局中与前者相反
  • weight来通过比例的方式设置控件的大小
    • 当两个控件水平排列,那么,这两个在一个布局中的控件分别设置weight的值为1,并且把wedth设置为0dp,那么就可以两个控件相同大小的宽度。
    • weight的计算原理,当前水平/竖直上的控件里面weight的值相加,然后每个控件的大小等于该控件的weight/总的weight,即是占据的比例大小。
    • 还可以通过weight与wedth组合设置宽度高度适配的布局

相对布局RelativeLayout

  • 相对于父布局
    • android:layout_centerInParent=”true”在父布局的中心
    • android:layout_alignParentBottom=”true”在父布局的最下面一列,在该列的位置不确定
    • android:layout_alignParentTop=”true”父布局最上面
    • android:layout_alignParentLeft=”true”在父布局左侧
    • android:layout_alignParentRight=”true”父布局的右侧
  • 相对于其他控件
    • android:layout_above=”@+id/bt1”在其他控件上面
    • android:layout_below=”@id/bt1”在其他控件下面
    • android:layout_toLeftOf=”@id/bt1”
    • android:layout_toRightOf=”@id/bt1”

页面内容滑动ListView

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1, date);
ListView listView = (ListView) findViewById(R.id.lv);
listView.setAdapter(adapter);

把字符串数组的内容当做一个个的框体显示,可以上下滑动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值