阅读Android官方教程 Google Training 1.1 ----- Buiding Your First APP

阅读谷歌官方教程第一章 Getting Started


这一节的内容都是最开始接触Android时的内容,但看过之后还是有所收益,下面我将会把我在各小节学到新知识进行简要说明


Buiding Your First APP

这一章通过建立默认的Hello World项目对Android各大文件,参数进行了说明,并结合一个通过EditText和Button发送信息到领体格Activity的例子,对布局和相关方法进行了说明

Creating an Android Project


  • Target Android Device 这一项是说你的APP所能运行的最最早版本的Android SDK版本,即所谓的API,为了去适应更多的Android设备,在允许展现APP核心功能的前提下,你应该把它设置成能达到的最低版本。

  • app/build.gradle 有为了整个项目的build.gradle,也有为系统各模块的build.gradle,通常我们只关心后者。

    • compileSdkVersion 是指你运行你的APP的平台版本,通常默认设置成你电脑上SDK的最新Android版本,你也可以设置成更旧的版本,但使用最新版本能让你使用新的功能并能让最新的Android系统使用者得到更好的体验。
    • applicationId 就是在新建项目向导时你指定的程序的 Package name
    • targetSdkVersion 指你测试APP的最高Android版本,随着版本更新,你可以更新它来获取新平台的优势。
  • drawable 为适应不同的屏幕密度的设计,不是存放图片的地方


Building a Simple User Interface

The graphical user interface for an Android app is built using a hierarchy of View and ViewGroup objects. View objects are usually UI widgets such as buttons or text fields. ViewGroup objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.

很好的阐述,是说app是建立在View和ViewGroup的层次关系上对象上面,View是各组件的父类,而ViewGroup则是不可见的View容器,定义了内部View的排列方式,比如网格或是一个竖直列表。

Android provides an XML vocabulary that corresponds to the subclasses of View and ViewGroup so you can define your UI in XML using a hierarchy of UI elements.

Android提供了View和ViewGroup的子类的XML语言,因此你可以通过UI元素的层次关系定义你自己的UI。

这里写图片描述 这张图清晰地展示了一种UI组成的层次关系

  • XML 属性
    • android id定义时的加号,比如 android:id=”@+id/edit_message”。这个加号只在第一次定义资源ID时使用,SDK会在编译的时候在编译时在gen/R.java文件自动生成相应对象的资源ID,你只需要在第一次创建时使用(在不同layout文件中创建被当做不同的情况),而其他情况下不需要使用+。
    • android layout_weight 权重值,指的是每个组件占剩余空间的比重,举个例子:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <EditText android:id="@+id/edit_message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:hint="@string/edit_message" />
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/button_send" />
</LinearLayout>

这是一个布局文件,里面包含了一个编辑框和一个按钮,如果在layout_width这个属性上用 wrap_content,在最终界面上每个组件的宽度是刚好包容下自身内容的宽度,如图所示:
这里写图片描述
在右边发现会空出一部分内容,因为两个组件都只考虑了自身宽度,为了更好的UI,我们可以这么改动输入框的布局文件,添加一个layout_weight属性。

<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

将宽度设为0,weight指设为1,这样的效果如下图所示:
这里写图片描述

我们可以发现,输入框恰当的填充了空余的宽度,为什么会这样,要先看官方对weight的解释:

The weight value is a number that specifies the amount of remaining space each view should consume, relative to the amount consumed by sibling views. This works kind of like the amount of ingredients in a drink recipe: “2 parts soda, 1 part syrup” means two-thirds of the drink is soda. For example, if you give one view a weight of 2 and another one a weight of 1, the sum is 3, so the first view fills 2/3 of the remaining space and the second view fills the rest. If you add a third view and give it a weight of 1, then the first view (with weight of 2) now gets 1/2 the remaining space, while the remaining two each get 1/4.

我弱弱地翻译一下,权重值指每个View应该占有剩余空间的比重,和其余成员所消耗的空间相关。如同饮料中配方的例子,2分苏打,1分糖浆,意味着2/3的饮料是苏打。如果你设置一个view的weight是2,另一个是1,总和是3,所以第一个组件将会填充剩余空间2/3的空间,第二个填满剩余1/3的空间,如果在添加view并设置权重为1,那么第一个view填充1/2,第二个和第三个个填充1/4。

weight的默认值是0,也就是说如果我们不设置,系统的默认值就是0,所以如果我们只设置了其中一个view的weight值,它必然将填满剩余空间。关于为什么设置width是0,官方解释如下:

Setting the width to zero (0dp) improves layout performance because using “wrap_content” as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.

意思是说,设置任何值都对系统计算宽度完全没有关联,因为权重值不为0,所以它会是一个填满剩余空间的宽度值。


Starting Another Activity

Build an Intent

官方讲了一个点击按钮将信息传递给另一个Activity的Intent示例

public class MainActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

我只看和Intent相关的内容
- this 其实第一个参数是Context,而Activity类是Context的一个子类。
- putExtra() 通过键值对的形式传递信息,对许多信息可用Extras(),细节大概都知道,关键是这个,官方指出,用你的APP的报名作为KEY的前缀是很好的习惯,这样保证了KEY的唯一性,不会与其他引用混淆。


关于另一个Activity的代码我也简单贴一下

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_display_message);

   Intent intent = getIntent();
   String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
   TextView textView = new TextView(this);
   textView.setTextSize(40);
   textView.setText(message);

   ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
   layout.addView(textView);
}
  • getIntet(),获取启动Activity的Intent
  • getStringExtra(),取得value值
  • ViewGroup 这个应该在布局文件就有,这里通过代码方式添加,应该要强制转换,应为view是所有布局的超类,强制转换后才能用addView()方法来动态添加组件。

没想到第一章第一节,我就写了这么多有的没的,以后的日子还怎么过啊,越看Google大神越觉得无知啊。。
本boy要去睡觉觉了,晚安。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值