Android Chinese translation, android Chinese translation Group

Source: Internet
Author: User
Tags home screen

Android Chinese translation, android Chinese translation Group

Zhang Yunfei VIR translation from: https://developer.android.com/training/basics/actionbar/index.html

Adding the Action Bar)

Note: I cannot find a better vocabulary Translation action bar. Although I think the activity bar is not a good translation, I always have a Chinese name. However, for easy identification, this article continues to use the English actionbar

The action bar in the activity bar is one of the most important design elements. You can implement it for the activity in your app. It provides several user interface features, which make your app feel friendly immediately and provide consistency in different Android apps. Key features include:

  • A dedicated area provided by your app, and the current location of the user is indicated in the app.
  • Access important activities in a predictable way (such as searching)
  • Supports navigation and view switching (via tag or drop-down list)

This training course provides a quick guide on the basics of action bar. For more information about the features of actionbar, see the Action Bar guide.

 

Course

Set action bar

Learn how to add a basic action bar to your activity, whether your app only supports Android 3.0 or later or the same version as Android 2.1 (by using the Android Support Library)

Add activity button

Learn how to add and respond to user activities on the action bar.

Styling your action bar

Learn how to customize the display of your action bar

Floating action bar

Learn how to overwrite the action bar in front of your layout and allow seamless conversion when you hide the action bar.

------------------------------------------------

Zhang Yunfei translation from: https://developer.android.com/training/basics/actionbar/setting-up.html

Set action bar

In most of its basic forms, the action bar displays a title for the activity and an app icon on the left. Even in this simple form, the action bar is still useful for all activities. It tells users where they are and maintains a consistent identifier for the app.

 

Table 1. A simple action bar with app icons and activity titles.

 

To set a basic action bar, your app needs to use a topic with the action bar enabled. How to request such a topic depends on the minimum supported version of your app. This lesson is divided into two chapters based on your minimum supported Android version.

 

Only Android 3.0 and above are supported

From Android 3.0 (API level 11), the action bar is included in all the activities using Theme. Holo themes (or one of its descendants). WhentargetSdkVersionOrThe minSdkVersion attribute is set "11"And larger, it will be the default style.

Then, add the action bar to your activity and set this attribute to 11 or higher. For example:

<manifest ... >    <uses-sdk android:minSdkVersion="11" ... />    ...</manifest>

Note: If you create a custom topic, make sure that it uses a Theme. Holo topic as its parent topic. For more details, see Styling the Action Bar.

Now Theme. Holo Theme is applied to your application, and all the activities show the action bar. That's it.

 

Support for Android 2.1 and later versions

When running a version earlier than Android 3.0 (down to version 2.1), to add an action bar, you must include the Android Support Library in your application ).

 

At the beginning, read the Support Library Setup documentation and setV7 appcompatLibrary (once you download the library package, follow the instructions for Adding the library Adding libraries with resources using resources ).

Once you integrate the android Support Library in your app project.

1. Update your activity so that it inherits fromActionBarActivity. For example:

public class MainActivity extends ActionBarActivity { ... }

2. Update the configuration file manifest.<application>Element or separate<activity>Element usesTheme.AppCompatTopic, for example:

<activity android:theme="@style/Theme.AppCompat.Light" ... >

Note: If you create a custom topic, make sure that it uses a Theme. Holo topic as its parent topic. For more details, see Styling the Action Bar.

Now your activity contains actionbar, when it runs on Android 2.1 (API level 7) or higher.

Remember to set the API level supported by your app in the manifest file:

<manifest ... >    <uses-sdk android:minSdkVersion="7"  android:targetSdkVersion="18" />    ...</manifest>

Next lesson: Add action. NEXT: ADDING ACTION BUTTONS

----------------------------------------------------------

Zhang Yunfei translation from: https://developer.android.com/training/basics/actionbar/adding-buttons.html

Add Action (Adding Action Buttons)

The action bar allows you to add buttons for the activity items (action items) associated with the current context of the app. The action buttons are known to show an icon and/or text activity directly in the action bar, and those that cannot be filled in the actionbar or are not important enough to be hidden in the overflow part of the action.

(Note: The icon or text we can see is the button action button. Invisible is the hidden extension button action overflow)

Figure 1An action bar contains an action button for search and an action Overflow Section, which shows the extension button.

Specify activity in XML (Specify the Actions in XML)

All action buttons and other available items (action overflow) in the extended button are defined in an XML menu resource. To add an action to the action barres/menu/Create an XML file under the folder.

Add the <item> element for each item that you want to include in the action, for example:

Res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- Search, should appear as action button -->    <item android:id="@+id/action_search"          android:icon="@drawable/ic_action_search"          android:title="@string/action_search"          android:showAsAction="ifRoom" />    <!-- Settings, should always be in the overflow -->    <item android:id="@+id/action_settings"          android:title="@string/action_settings"          android:showAsAction="never" /></menu>

When there is enough space for the search activity on the action bar in this declaration, it will be displayed as the action button, but the setting activity will always appear in the alternative (extended) button. (By default, all actions appear in the form of alternative buttons, but it is a good practice to explicitly declare your design purpose for each action activity .)

 

The icon attribute must specify the resource ID of an image. The name following @ drawable/must be the name of an image saved in the res/drawable/directory of your project. For example"@drawable/ic_action_search"ReferenceIc_action_search.png. Similarly,titleAttribute uses a string resource, which has been defined in an XML file in the res/values/directory of your project. For more information, see Building a Simple User Interface.

Note: When you create an icon or other bitmap image in your app, it is very important to provide the optimized multi-version for different screen density. For more information, see Supporting Different Screens.

If you use the Support Library to be compatible with versions like android 2.1, the showAsAction attribute is unavailable in android: namespace. This attribute method is provided by the Support Library. You must define your own XML namespace and use that namespace as the attribute prefix. (A custom namespace should be based on the name of your app ). However, it can be any name you want to define, and can be used only in the file cycle. For example:

Res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >    <!-- Search, should appear as action button -->    <item android:id="@+id/action_search"          android:icon="@drawable/ic_action_search"          android:title="@string/action_search"          yourapp:showAsAction="ifRoom"  />    ...</menu>

 

Download Action bar icon

To better match the android iconography guide, you should use the icons provided in the Action Bar Icon Pack.

 

Add activity to action bar

You need to place menu items to the total action bar to implementonCreateOptionsMenu()Callback method: Fill in the Menu resources to the specified Menu object, for example:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu items for use in the action bar    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.main_activity_actions, menu);    return super.onCreateOptionsMenu(menu);}

 

Response to button

When you click the action button or extension button in an action bar, the system callsonOptionsItemSelected()Callback method. In your implementation of this method, call the MenuitemThe getItemId () method determines which item is clicked. The ID returned by this method matches a value. This value is android, which is already in the corresponding <item> element: id.

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    // Handle presses on the action bar items    switch (item.getItemId()) {        case R.id.action_search:            openSearch();            return true;        case R.id.action_settings:            openSettings();            return true;        default:            return super.onOptionsItemSelected(item);    }}

 

Add Up button for low-level activity

In all the screens in your app, those that are not the main portal (not the Home screen activity) you should be provided with a way to navigate to a logical parent screen in the app hierarchy, which can be done by clicking the Up button on the action bar.

 

When running in Android 4.1 (API level 16) or higher, or when usingActionBarActivityYou need to declare the parent activity and enable the Up button for the action bar in the manifest file.

 

For example, how do you declare the parent activity of an activity in the manifest file.

<application ... >    ...    <!-- The main/home activity (it has no parent activity) -->    <activity        android:name="com.example.myfirstapp.MainActivity" ...>        ...    </activity>    <!-- A child of the main activity -->    <activity        android:name="com.example.myfirstapp.DisplayMessageActivity"        android:label="@string/title_activity_display_message"        android:parentActivityName="com.example.myfirstapp.MainActivity" >        <!-- Parent activity meta-data to support 4.0 and lower -->        <meta-data            android:name="android.support.PARENT_ACTIVITY"            android:value="com.example.myfirstapp.MainActivity" />    </activity></application>

In this casesetDisplayHomeAsUpEnabled() Enable the app icon as the Up button:

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_displaymessage);    getSupportActionBar().setDisplayHomeAsUpEnabled(true);    // If your minSdkVersion is 11 or higher, instead use:    // getActionBar().setDisplayHomeAsUpEnabled(true);}

Because the system has automaticallyThe parent activity of DisplayMessageActivity isMainActivityClickUp button, The System navigation to the corresponding parent activity-you do not need to processUpButton event.

For more information about the up button, see Providing Up Navigation.

NEXT lesson: STYLING your action bar NEXT: STYLING THE ACTION BAR

 

Bytes ------------------------------------------------------------------------------------------

Zhang Yunfei VIR, https://developer.android.com/training/basics/actionbar/styling.html

Styling your action bar

Action bar provides your users with a familiar and predictable way to process activity actions and navigate your app, but this does not mean that it should look exactly the same as other apps. If you want to style your action bar and better adapt to your product brand, you can easily use styles and theme resources.

Android contains some built-in activity themes, including dark "dart" and light "action bar. You can inherit these topics to further customize the way your actionbar looks.

Note: If you use the Support Library API for actionbar, you must use (or overload override) Theme. AppCompat family themes (equivalentTheme.HoloFamily, available at API level 11 or higher ). In this way, each declared style attribute must be declared twice: one is the platform's style attribute (using android: attribute ), and one-time use of the style attributes contained in the Support Library (using appcompat. r. attr attributes-the context of those attributes is actual ). The following example provides more details.

 

Use an android topic

Android provides two baseline themes for the action bar:

  • Theme. Holo is a dark (black) Theme.
  • Theme. Holo. Light (white, bright) Theme.

 

 

 

You can apply these themes to your entire app or a single activity. You can use the android: theme attribute in the manifest file to act on the <application> element or a single <activity> element.

For example:

<application android:theme="@android:style/Theme.Holo.Light" ... />

You can also declareTheme.Holo.Light.DarkActionBarTo use a dark actionbar, while all the remaining activities use a light color scheme.

 

When using the Support Library, you must useTheme.AppCompatTopic instead:

  • Theme.AppCompatDark theme.
  • Theme.AppCompat.LightLight-colored theme.
  • Theme.AppCompat.Light.DarkActionBarThe light-colored theme, but the action bar is dark.

Make sure that you use an action bar icon that is in contrast to the color of your action bar. To help you, the action bar Icon Pack contains the standard Action icons for the action Bar Holocaust light and Holo dark.

 

Custom background

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <! -- The theme applied to the application or activity --> <style name = "customaction1_eme" parent = "@ android: style/Theme. holo. light. darkActionBar "> <item name =" android: actionBarStyle "> @ style/MyActionBar </item> </style> <! -- ActionBar styles --> <style name = "MyActionBar" parent = "@ android: style/Widget. holo. light. actionBar. solid. inverse "> <item name =" android: background "> @ drawable/actionbar_background </item> </style> </resources>

In this case, apply your topic to the entire app or a single activity:

<application android:theme="@style/CustomActionBarTheme" ... />

 

Support for Android 2.1 and later

When using the Support Library, the same style as above must be replaced with the following:

Res/values/themes. xml

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@style/Theme.AppCompat.Light.DarkActionBar">        <item name="android:actionBarStyle">@style/MyActionBar</item>        <!-- Support library compatibility -->        <item name="actionBarStyle">@style/MyActionBar</item>    </style>    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">        <item name="android:background">@drawable/actionbar_background</item>        <!-- Support library compatibility -->        <item name="background">@drawable/actionbar_background</item>    </style></resources>

In this case, apply your topic to the entire app or a single activity:

<application android:theme="@style/CustomActionBarTheme" ... />

 

Custom text color
  • To modify the text color in the actionbar, You need to overwrite the independent attributes for each text element.
  • Action bar title: create a custom style with the textColor attribute specified, and usetitleTextStyleAttribute indicates the style.
    • Note: It is appliedThe custom attribute of titleTextStyle should be used TextAppearance. Holo. Widget. ActionBar. Title is used as the parent style.
  • Action bar tab (tabs): overwrite it in your activity StyleactionBarTabTextStyle 
  • Action button: overwrite your activity StyleactionMenuTextColor 
Only Android 3.0 and later are supported

If only Android 3.0 and later are supported, your style XML file can be as follows:

Res/values/themes. xml

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@style/Theme.Holo">        <item name="android:actionBarStyle">@style/MyActionBar</item>        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>        <item name="android:actionMenuTextColor">@color/actionbar_text</item>    </style>    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@style/Widget.Holo.ActionBar">        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>    </style>    <!-- ActionBar title text -->    <style name="MyActionBarTitleText"           parent="@style/TextAppearance.Holo.Widget.ActionBar.Title">        <item name="android:textColor">@color/actionbar_text</item>    </style>    <!-- ActionBar tabs text styles -->    <style name="MyActionBarTabText"           parent="@style/Widget.Holo.ActionBar.TabText">        <item name="android:textColor">@color/actionbar_text</item>    </style></resources>
Only Android 2.1 and later are supported

When using the Support Library, your style XML file can be like this:

Res/values/themes. xml

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@style/Theme.AppCompat">        <item name="android:actionBarStyle">@style/MyActionBar</item>        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>        <item name="android:actionMenuTextColor">@color/actionbar_text</item>        <!-- Support library compatibility -->        <item name="actionBarStyle">@style/MyActionBar</item>        <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>        <item name="actionMenuTextColor">@color/actionbar_text</item>    </style>    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@style/Widget.AppCompat.ActionBar">        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>        <!-- Support library compatibility -->        <item name="titleTextStyle">@style/MyActionBarTitleText</item>    </style>    <!-- ActionBar title text -->    <style name="MyActionBarTitleText"           parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">        <item name="android:textColor">@color/actionbar_text</item>        <!-- The textColor property is backward compatible with the Support Library -->    </style>    <!-- ActionBar tabs text -->    <style name="MyActionBarTabText"           parent="@style/Widget.AppCompat.ActionBar.TabText">        <item name="android:textColor">@color/actionbar_text</item>        <!-- The textColor property is backward compatible with the Support Library -->    </style></resources>

 

Custom tab (Label, tab) Indicator

 

To change the indicator used for the navigation label navigation tabs, create an activity topic that overwrites the actionBarTabStyle attribute. This property points to another one that you overwritebackgroundSpecifies a drawable object for the status list feature ).

Note: The Drawing Object of the status list feature is very important. The background of the selected status label is different from that of other labels.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.