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.
In 项目浏览框
, find res目录
, right click, select--- new
android resource file
;
In the corresponding field according to fill in;
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 ;
-
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 ;}
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
In the 项目浏览
window, find the program source of the package, right click, select,---- new
Activity
Gallery...
;
Choice Empty Activity
,
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.
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>
关于
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 ;
-
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;}
-
In res\values\strings.xml
, add a format string,
<resources > <string name = "Verion_info" ; Version:%s \ n Designer:anddle </string ; </resources ;
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