之前已经创建好了项目:
【记录】创建第一个Android程序: Building Your First App
创建了AVD:
【记录】创建AVD(Android Virtual Device)安卓虚拟设备
运行了app:
现在,接着就是去,实现安卓版的DownloadSongtasteMusic中的,文本标签,和输入框了。
1.参考:
Building a Simple User Interface
去看看,如何创建对应的简单的UI。
2.双击src/layout/activity_main.xml,默认是通过Graphical Layout打开的,点击以切换到activity_main.xml的文本编辑模式:
2.再参考教程,把代码改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" tools:context = ".MainActivity" > <!-- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> --> </ LinearLayout > |
3.期间,写了如下代码:
1 2 3 4 5 6 | < EditText android:id = "@+id/url_or_id" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:hint = "@string/songtaste URL or ID" > |
结果出现一个警告,暂时记录与此:
4.然后顺便去看了,对应的R.java中的效果:
可以看到,的确已经同步创建了对应的变量url_or_id了。
5。从教程中的介绍得知,此处用加号,表示新创建的resource
而对于string和layout,是不需要的。(静态时,就已创建好了)
6.看了后面的解释,才知道,上述的:
android:hint="@string/songtaste URL or ID"
写法是错误的,应该是:
android:hint="@string/url_or_id"
其中,此处string下面的url_or_id,和之前:
android:id="@+id/url_or_id"
中的id下面的url_or_id,由于属于不同的scope,可以理解为namespace,
所以两者并不是同一个变量,而是不同的变量(资源);
对应的,string中的url_or_id,后续代码会另外定义其值的。
7.再去打开res/values/strings.xml:
添加一个新的:
按Ctrl+S,保存一下。
8.接着继续去编辑xml文件,添加变量btn_download:
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "app_name" >Download Songtaste Music</ string > < string name = "menu_settings" >Settings</ string > < string name = "url_or_id" >Input Songtaste URL or ID</ string > < string name = "btn_download" >Download</ string > </ resources > |
期间遇到一个错误,详情参考:
【已解决】ADT中编辑strings.xml时出现错误:content is not allowed in prolog
9.目前的代码是:
/DownloadSongtasteMusic/res/layout/activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" tools:context = ".MainActivity" > <!-- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> --> < EditText android:id = "@+id/url_or_id" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:hint = "@string/url_or_id" /> < Button android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/btn_download" /> </ LinearLayout > |
/DownloadSongtasteMusic/res/values/strings.xml
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "app_name" >Download Songtaste Music</ string > < string name = "menu_settings" >Settings</ string > < string name = "url_or_id" >Input Songtaste URL or ID</ string > < string name = "btn_download" >Download</ string > </ resources > |
然后点击Run,运行的结果是:
10.添加了
android:layout_weight="1"
相关代码变成:
1 2 3 4 5 6 | < EditText android:id = "@+id/url_or_id" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:hint = "@string/url_or_id" android:layout_weight = "1" />" |
然后Run,结果是:
11.再改为0dp:
1 2 3 4 5 6 | < EditText android:id = "@+id/url_or_id" android:layout_width = "0dp" android:layout_height = "wrap_content" android:hint = "@string/url_or_id" android:layout_weight = "1" />" |
运行后效果,和之前一样:
【总结】
1.通过
res/layout/activity_main.xml
控制,有什么:
文本输入框
按钮
等等;
其中不同的layout方面的参数配置,决定了位置,宽度,高度等显示内容;
2.通过
res/values/strings.xml
去设置对应的string变量值。