Teach you to do the Beans Calculator (vii)-"About" interface

Source: Internet
Author: User

Section 8th about the interface

Now we're going to add a self-introduction to the app, and self-introduction is also an activity that starts from the menu bar in the top right of the app.

8.1 Adding a Menu

Apply the area of the title bar, called Actionbar . This is used to display the name of the app and to provide various menu of actions for the app. We're going to add a 关于 menu here.

  1. In 项目浏览框 , find res目录 , right click, select--- new android resource file ;

  2. In the corresponding field according to fill in;

  3. Add a menu item to the newly created XML file, the showAsAction property indicates whether the menu is displayed directly, and if assigned never , it will be folded to the upper right corner, and the 三个点 property is used to specify the title text to be displayed for the menu item, which we use as a string reference. Set the menu bar text toAbout

    <?xml version= "1.0" encoding= "Utf-8"?  <menu  xmlns:android  = "Http://schemas.android.com/apk/res /android " xmlns:apps  =" HTTP +/ Schemas.android.com/apk/res-auto ";  <item  apps:showasaction  = "never"  android:id  = "@+id/menu_about"  android:title  =" @string/about "/>  </menu ;   
  4. In the source code of the Mainactivity.java , add the menu item to Actionbar ,

     @Override  public     boolean  oncreateoptionsmenu  (Menu menu) {    Getmenuinflater (). Inflate (R.menu.menu, menu); return  true ;}  
  5. In MainActivity.java the source code, add to the menu item click on the response,

    @OverridepublicbooleanonOptionsItemSelected(MenuItem item) {    switch (item.getItemId())    {        case R.id.menu_about:        {                //这里添加启动“关于”界面的代码。        }        break;    }    returntrue;}

At this point, the menu function is added, and after we have created the "about" interface activity, then to modify the menu click Response, let it launch the "about" interface.

8.2 Activity Add 8.2.1 Create activity
    1. In the 项目浏览 window, find the program source of the package, right click, select,---- new Activity Gallery... ;

    2. Choice Empty Activity ,

    3. Set the name of the activity and layout file as shown in the table below,

In this way, the activity-related code and layout files are created.
In the process of creation, the application is automatically added to the AndroidManifest.xml following content, the newly added activity is registered, otherwise, the activation of the activity, the system will error.

<applicationandroid:allowbackup="true"android:icon="@ Mipmap/ic_launcher "android:label=" @string/app_name "android:supportsrtl= "true" android:theme="@style/apptheme">                        <activity android:name=". Mainactivity ">        <intent-filter>            <action android:name="Android.intent.action.MAIN" />            <category android:name="Android.intent.category.LAUNCHER" />        </intent-filter>    </activity>    <activity android:name=". Aboutactivity "></activity></Application>
8.2.2 Creating activity Layouts

In the layout we add the version information and the user information.

  1. Modify the res\layout\activity_about.xml file, use TextView to display the user and version information, background color used previously defined #FF4B5459 ;

    <?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:orientation  =" vertical " android:layout_width  = "match_parent"  android:layout_height  = "match_parent"  android:background  =" @color/COLORAPPBG ";     <TextViewandroid:layout_width="Match_parent"android:layout_height= "Match_parent" android:id="@+id/version_info"style="@style/aboutcontentstyle"/>                                </linearlayout>
  2. 关于defines a style for the text displayed on the interface, and then for TextView specifying this style,

    < Style  name  = "Aboutcontentstyle"  >   <item  name  = "android  :textcolor " >@COLOR/COLORDISPLAYTEXT</ITEM>  <item name= "Android : TextSize " > @dimen/displaytextsize</item> <item name=" Android: Padding " >5  dp</item> <item name=" Android:gravity " >center</item>   </ style ;   
  3. In the onCreate () function in Aboutactivity.java , use Packagemanager to obtain the installed application information, and get the version number; The code is as follows,

     @Override  protected  void  onCreate     (Bundle savedinstancestate) {super . OnCreate (savedinstancestate);    Setcontentview (r.layout.activity_about);    Packagemanager manager = Getpackagemanager ();    PackageInfo info = null ; try     {info = Manager.getpackageinfo (Getpackagename (), 0 );    } catch  (packagemanager.namenotfoundexception e) {e.printstacktrace (); } String Version = info = = null ? GetString (R.string.unknown): info.versionname;}  
  4. In res\values\strings.xml , add a format string,

     <resources  >  <string  name  = "Verion_info" ;  Version:%s \ n Designer:anddle </string ;  </resources ;   
  5. Using a formatted string, String.format() you can %s replace it with a string that we want to replace.

    @Override?protectedvoidonCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    ......    String msg = String.format(getString(R.string.verion_info), version);    TextView ver = (TextView) findViewById(R.id.version_info);    ver.setText(msg);}
8.2.3 Start activity

Finally, in the response function of the menu item, the newly created activity is started up.
Initiate another activity that can be provided by activity startActivity() ;
Here you need to create a first Intent , fill in the class name of the activity to be started by intent,

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {    switch (item.getItemId())    {        case R.id.menu_about:        {            new Intent(this, AboutActivity.class);            startActivity(i);        }        break;    }    returntrue;}

On the device to run the program, you can see the "about" interface successfully added and displayed.

Teach you to do the Beans Calculator (vii)-"About" interface

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.