Android 第二周笔记

问题1:当有界面中有多个按钮进行事件处理的时候?

1.让MainActivity去实现OnClickListener接口
2.实现onClick方法

@Override
public void onClick(View v) {
	// TODO Auto-generated method stub
	//参数v 我们点击的控件对象
	//1.通过V要去判断到底室哪个按钮
	switch(v.getId()){
	case R.id.button1:
		
	case R.id.button2:
	case R.id.button3:
	case R.id.button4:
	}
	
}

3.给每一个按钮设置事件监听器

    bt1.setOnClickListener(this);
	bt2.setOnClickListener(this);
	bt3.setOnClickListener(this);
	bt4.setOnClickListener(this);

问题2:颜色资源

**res—values—colors.xml**
<resources>
    <color name="colorPrimary">#00574B</color>
</resources>

java代码中使用颜色资源:

tv.setTextColor(getResources().getColor(R.color.colorPrimary));

布局文件中使用颜色资源:

android:textColor="@color/colorPrimary"

新属性:

android:layout_weight 设置权重
一般情况下 
如果控件是横向排列的,android:layout_width="0dp"
如果控件是纵向排列的, android:layout_height="0dp"
能够严格保证控件的大小按照权重分配

否则的,权重设置后的显示效果会受宽度和高度的原来设置的值不同,效果也不同

控件:
EditText:
父类:TextView
子类:AutoCompleteTextView 自动补全功能文本框 ExtractEditText 输入法
属性:显示提示信息 android:hint
显示密码:android:inputType textPassWord numberPassWord
在控件上文在的上下左右加图片:android:drawableLeft android:drawableRight

案例:简易计算器

界面分析:
1.整体:纵向排列的几个控件,TextView/EditText,若干个线性布局——垂直线性布局
2.局部:每一行按钮,水平线性布局

提示:
1.注意线性布局嵌套的用法
2.每一个线性布局都要设置宽度和高度
3.合理使用android:layout_weight、android:layout_gravity、android:gravity属性让你的布局更美观合理
4.小心属性值设置时的互相影响,不如宽度、高度设置match_parent或wrap_content时对其他属性值的影响

需要解决的几个问题:
1.出现的错误:

Wrong orientation? No orientation specified, and the default is horizontal, yet this layout has 
 multiple children where at least one has layout_width="match_parent"

2.各种资源的抽取

在布局文件中不要出现文字、字符串、颜色值、距离、大小的硬编码,都使用资源的形式

3.根号的显示问题
ImageView
父类:View
(1)android:background:背景——继承自View
(2)android:src:设置要显示的图片资源

注意:(1)图片的放置位置:一般放在xhdpi或者hdpi文件夹下
(2) 图片的名字:必须跟java的命名规则一致,原因:这个图片名会成为R文件中生成的常量的名字
(3) android:scaleType=“fitStart”:设置图片的缩放或移动类型

4.显示计算过程的控件外面的框怎么实现?

基本形状的定义工具:shape
1.形式和位置
xml文件 drawable目录下
2.有哪些形状?在哪里设置?
(1)rectangle:矩形,默认的形状
(2)oval:椭圆型,经常用来画正圆
(3)line:线型,直线、虚线
(4)ring:环形,可以画环形进度条

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">
    

</shape>

3.具体显示效果
(1)corners:圆角,常用
(2)gradien:渐变
(3)padding:间隔
(4)size:尺寸
(5)solid:填充,常用
(6)stroke:描边,常用

圆角矩形、红色填充、黑色虚线边

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 圆角  半径为10dp 4个角一样 -->
    <corners android:radius="10dp" />
 	<!-- 填充红色 -->
    <solid android:color="@color/color_red"/>
	<!-- 描边  宽度为2dp 颜色黑色  虚线的间隔4dp 虚线的宽度4dp  后两个属性有一个是0 就代表是实线 -->
    <stroke
        android:width="2dp"
        android:color="#000000"
        android:dashGap="4dp"
        android:dashWidth="4dp"/>

</shape>

4.形状的使用
凡是能够放图片的属性,都可以放一个形状

<Button
        android:id="@+id/bt3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/bt_bt3"
        android:textColor="#00ff00" 
        android:background="@drawable/button_shape"
        />

android:src都可以

5.如何实现控件不同操作下显示不同的效果?

选择器:selector

1.选择器的种类
drawable selector 图片选择器
color selector 颜色选择器

2.图片选择器
(1)形式及存放的位置
xml文件 res-drawable目录下

(2)要实现的内容
不同状态下要显示的图片

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 按压时 -->
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
    <!-- 默认时 -->
    <item android:drawable="@drawable/button_normal"/>
</selector>

注意:默认状态下的设置,也就是说item里面 没有设置state_XXXXX的这种状态,一定写在最后面

(3)使用方式

3.颜色选择器

与图片选择器不一样的地方:位置不一样res/color目录下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@android:color/holo_red_light"/>
    <item  android:color="#000000"/>
</selector>

使用方式

<Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左边的按钮"
        android:layout_gravity="left" 
         android:textColor="@color/text_color" 
        android:background="@drawable/button_selector"/>

AS中没有drawable-xxhdpi目录:
res——右键——Android Rescourse Directory——rescourse Type 选 drawable——directory name:drawable-xxhdpi

6.每个按钮的属性值基本相同,带来的问题:当我们要修改按钮的属性值时需要修改很多地方
解决方案:style样式

位置:res—values—styles.xml

<style name="button_style">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/text_color</item>
        <item name="android:background">@drawable/button_shape</item>
    </style>

使用:

 <Button
        android:id="@+id/bt3"
        android:text="@string/bt_bt3"
        style="@style/button_style"
        />

7.Theme主题

就是窗体的样式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值