Getting_Started-----Build Your First App

Build Your First App

        本文出自:http://developer.android.com/training/basics/firstapp/index.html

 

        1.build App相关注意点

        You should always set the android:targetSdkVersion as high as possible and test your app on the corresponding platform version. For more information, read Supporting Different Platform Versions.

        2.关于“+”(android:id="@+id/edit_message")

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal">
   <EditText android:id="@+id/edit_message"
             android:layout_weight="1"
             android:layout_width="0dp"
             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>

         The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's gen/R.java file that refers to the EditText element. Once the resource ID is declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying(指定) a new resource ID and not needed for concrete resources such as strings or layouts。

         3.布局小技巧:关于weight

        android:layout_weight属性是指:例如你给一个view的该值为2,另一个视图权重为1,则总和为3.因此,第一个view填充剩余空间的2/3,第二个view填充剩余的空间。如果你再加一个view,然后给其权重为1

那么第一个view(其android:layout_weight=2)现在将得到1/2的空间,另两个view将分别占据1/4空间。

        所有view的默认weight值为0,因此如果你仅仅给一个view的weight赋值一个大于0的值,那意思即所有的视图被给其要求的空间后,该视图填充剩下的空间。

        例如如下图和布局(未使用weight属性之前):

<LinearLayout         
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

       <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>

 
Getting_Started-----Build Your First App
 Figure 1. The <a style="color: #258aaf;" href="http://developer.android.com/reference/android/widget/EditText.html">EditText</a> and <a style="color: #258aaf;" href="http://developer.android.com/reference/android/widget/Button.html">Button</a> widgets have their widths set to "wrap_content".

          你会发现该布局可能不好,因为当用户在编辑框输入过长的内容时,send按钮会一直右移。你可以使用weight属性进行优化布局.为了使EditText填充LinearLayout布局剩余的空间,设置layout_weight=1,同时不设置button的weight值。

        当你设置了weight属性时为了提高布局效率,你应该设置android:layout_width="0dp"。设置width为0提高了布局性能,因为如果使用“wrap_content”作为EditText的高度,系统将会计算EditText布局所需的高度。而这时无用的工作。因为其设置了weight,那要求重新再计算EditText的宽度以填充Button布局后的剩余空间。

        最后完整的布局和展示图如下:     

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        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>


Getting_Started-----Build Your First App
 Figure 2. The <a style="color: #258aaf;" href="http://developer.android.com/reference/android/widget/EditText.html">EditText</a> widget is given all the layout weight, so fills the remaining space in the <a style="color: #258aaf;" href="http://developer.android.com/reference/android/widget/LinearLayout.html">LinearLayout</a>.

        4.关于Intent里key值的定义

        In order for the next activity to query the extra data, you should define the key for your intent's extra using a public constant. So add the EXTRA_MESSAGE definition to the top of the MainActivity class:

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    ...
}

       It's generally a good practice to define keys for intent extras using your app's package name as a prefix. This ensures they are unique, in case your app interacts with other apps

       

      5.关于Fragment

        ..........

        Fragments decompose(分解) application functionality and UI into reusable modules.

        ...........

       6.关于ADT

        Note: Your activity may look different if you did not use the latest version of the ADT plugin. Make sure you install the latest version of the ADT plugin to complete this tutorial.

        7.关于activity声明新特性

<application ... >
    ...
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>

 

        The android:parentActivityName attribute declares the name of this activity's parent activity within the app's logical hierarchy. The system uses this value to implement default navigation behaviors, such as Up navigation on Android 4.1 (API level 16) and higher. You can provide the same navigation behaviors for older versions of Android by using the Support Library and adding the <meta-data> element as shown here.

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

       8.关于activity布局设置

        To show the message on the screen, create a TextView widget and set the text using setText(). Then add the TextView as the root view of the activity’s layout by passing it to setContentView().

        The complete onCreate() method for DisplayMessageActivity now looks like this:

        

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
}

相关推荐