Android开发5个小常识:设置按键效果等

    技术2022-07-11  78

    转自:https://mobile.51cto.com/abased-374319.htm

    记载你或许想要了解的关于android的一些小知识:

    1.为按钮设置简单的按键效果。

    比如,在drawable文件夹中创建camera.xml,内容如下:

    <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/camera_press"  android:state_pressed="true"/> <item android:drawable="@drawable/camera_press" android:state_focused="true"/> <item android:drawable="@drawable/camera_normal" /> </selector>

    代表触碰到及按下时图片为camera_press,常态下图片为camera_normal。

    然后在layout布局文件中这样写即可: 

    <TextView android:id="@+id/camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/camera" /> 

    2.国际化

    见上图文件目录,strings.xml一份放在values-zh下,默认的英文strings.xml放在values下。

    图片的默认放在drawable-hdpi,包含中文的那几张图片放在drawable-zh-hdpi下。

    这样android系统会根据用户选择的语言自动找到对应的资源文件。

    3.R文件不能自动编译

    一般是因为res下的layout文件包含错误,解决掉了就好了。

    4.关于布局

    个人认为RelativeLayout还是很好用的,常用属性如下:

    android:gravity="center"代表组件的内容水平并且垂直居中

    android:layout_alignParentTop="true"代表处于上级标签,即父组件的顶部

    android:layout_margin="5dp"代表与父组件上下左右有5dp的间距

    android:padding="10dp"代表该组件包含的内容与该组件本身上下左右有10dp的空隙

    5.对话框

    Builder builder = new AlertDialog.Builder(this)这种方式建立的dialog貌似不好调用自定义的取消按钮,只有使用Dialog dialog = new Dialog(this)才能使用dialog.dismiss()方法来自定义取消按钮。

    Processed: 0.016, SQL: 9