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>
Figure 1. The EditText
and Button
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>
Figure 2. The EditText
widget is given all the layout weight, so fills the remaining space in the LinearLayout
.
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);
}
相关推荐
You can make the most of the Polymer Designer Tool app and create your own app without coding at all. Finally, you will see how you can improve your Polymer application by reading the best practices ...
Install host build dependencies mentioned in Getting Started guide. Add tools/ directory to the PATH Run python -m pip install -r requirements.txt to install Python dependencies Configuring the ...
Chapter 4: Creating Your First Android App Chapter 5: Polishing Your App by Adding Menus and Preferences Chapter 6: Deciphering Secret Messages Chapter 7: Creating Advanced GUIs and Sharing Your App ...
you can first adjust your Chart in the app and then implement it in code) 1 - Getting Started By this instructions you can add this library and i will explain how use it. Add Maven to your root ...
You can be a first mover in this amazing app revolution, and build Windows Store apps that could launch your career. This guide will point you to the right developer tools, tutorials, and resources. ...
Getting Started Chapter 2. Building Our First App - 7 Minute Workout Chapter 3. More Angular 2 – SPA, Routing, and Data Flows in Depth Chapter 4. Personal Trainer Chapter 5. Supporting Server Data ...
Chapter 10: Getting Started with Web Services Chapter 11: Creating a Universal App Chapter 12: Deploying Your iOS App Appendix: Answers to Exercises Book Details Title: Beginning iOS Programming: ...
You will then learn to build your first Twitter-like app. Later on, you will be introduced to Hashtags, AJAX to enhance the user interface, and tweets. You will then move on to create an ...
$client->setDeveloperKey("YOUR_APP_KEY"); $service = new Google_Service_Books($client); $optParams = array('filter' => 'free-ebooks'); $results = $service->volumes->listVolumes('Henry David Thoreau',...
Chapter 2: Getting Started with Google Cloud Platform Chapter 3: Using Google APIs Part II: Google Cloud Platform - Compute Products Chapter 4: Google Compute Engine Chapter 5: Google App Engine ...
Chapter 2: Getting Started with Android Chapter 3: Speaking Java – Your First Game Chapter 4: Discovering Loops and Methods Chapter 5: Gaming and Java Essentials Chapter 6: OOP – Using Other People'...
Getting Started Chapter 2. Creating a Solid Scaffolding Chapter 3. Yes, You Should Go Mobile First Chapter 4. Applying the Bootstrap Style Chapter 5. Making It Fancy Chapter 6. Can You Build a Web ...
Building Slack Bots by Paul Asjes 2016 | ISBN: 1786460807 | English | 216 pages | EPUB | 8 MB Key Features: This is the first developer's guide to programming for Slack ...Publishing Your App
Getting Started Chapter 2. Implementing Your First Custom View Chapter 3. Handling Events Chapter 4. Advanced 2D Rendering Chapter 5. Introducing 3D Custom Views Chapter 6. Animations Chapter 7. ...