In our last blog, the most basic framework has been set up, then why do we call this article frame building? The reason is that we built a framework that runs through this project, to manage the declaration cycle of the project, and what we need to do next is to build a framework that we need to be content-related.
Let's take a look at the final results of our framework:
In this framework, we need to do the following work:
- 1: Add a Tabhost,tab are homepage, category, rank, recommendation and theme respectively
- 2: First page has a user button, search box and QR Code scan button
- 3: Add skid Bar
- 4: Click on each button to access the corresponding interface
Here's one of the things we do:
(a): Add Tabhost
Honestly, Tabhost is very difficult to use, so in the current Android API is not recommended to use Tabhost, but, in fact, it is relatively simple to use, so this time I used the API.
Without saying much, we begin:
(1): Add layout
1: We need to create a new XML file under the Res/layout folder, named Main_tab.xml, and choose Layout as Tabhost
2: The classmate who used Tabhost may be very clear, the use of tabhost is very fixed.
The basic way is that the root layout is tabhost, there is a layout manager, generally relativelayout, and then in the layout manager there is a framelayout to store the corresponding content, and finally a tabwidget, used to display the label.
3: Here are three places to be aware of:
1:TabHost的id必须要这样设置** android:id="@android:id/tabhost" **2:中间的FrameLayout的id必要要设置为** android:id="@android:tabcontent" **3:最后的TabWidget的id必须设置为** android:id="@android:id/tabs" **
Here's a look at the overall layout code:
<?xml version= "1.0" encoding= "Utf-8"?> <tabhost xmlns:android="Http://schemas.android.com/apk/res/android" Android:id="@android: Id/tabhost"android:layout_width="Match_parent"Android : layout_height="Match_parent" > <!--need a layout manager -- <relativelayoutandroid:layout_width="Fill_parent"android:layout_ Height="Fill_parent" > <framelayoutandroid:id="@android: Id/tabcontent"android:layout_width ="Fill_parent"android:layout_height="Fill_parent" > </framelayout> <!--Tabwidget must be labeled to hold the tab tag, and the ID must be tabs-- <tabwidgetandroid:id="@android: Id/tabs"android:layout_width=" Fill_parent "android:layout_height=" Wrap_content "android:layout_alignparentbottom ="true"android:background="@color/mbarcolor" > </tabwidget> </relativelayout> </tabhost>
In which a color attribute appears, Mbarcolor. Here's a look at:
There are many resources in Android that can be defined in a resource file or in a style file to be reused, including color, let's look at how to define your own color resources.
1:在res/values文件夹下面会有一个color.xml。(如果没有可以自己新建一个)。2:如果是新建的color.xml文件,则打开文件之后,需要添加xml根节点: <resource></resource>3:如果想要添加自己的color资源,就需要在resource节点下面添加color节点下面来看一下我们mbarcolor颜色的定义: <color name="mbarcolor">#29abe2</color>4:调用的时候,就可以使用名字调用了。
The layout file is set up, we see it on our own, and we need to build holder for our tabwidget.
Our holder is above a imageview, below a textview.
1:在res/layout文件夹下面新建tab_layout.xml文件2:添加代码```<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <ImageView android:id="@+id/tab_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" /> <TextView android:id="@+id/tab_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" /></LinearLayout>```
Well, Tabhost's layout file is set up, so let's use that layout file now.
1: Create a new Markettab.java file in our Src/com.sdu.activities package.
2: Get the appropriate control and create a resource file
Private Tabhost Tabhost;Resource file private Class activitys[] = {homeactivity. Class, sortactivity. Class, hotactivity. Class, recommandactivity. Class, themeactivity. Class};//Jump ActivityPrivate String title[] = {"Home","category","Ranking","Recommended","Subject"};//The title of the settings menuprivate int image[] = {R. drawable. Tab_home, R. drawable. Tab_sort, R. drawable. Tab_hot, R. drawable. Tab_rec, R. drawable. Tab_sort};//Settings menu
2: New Inittab function, initialize operation
private void InitTab () {Resources resources = getresources ();Tabhost = (tabhost) Findviewbyid (Android. R. ID. Tabhost);Tabhost. Setup(This. Getlocalactivitymanager());Create a label for (int i =0; i < activitys.length; i++) {Instantiate a view as the tab label Layout view view = view. Inflate(This, R. Layout. Tab_layout, NULL);Set ImageView ImageView ImageView = (ImageView) view. Findviewbyid(R. ID. Tab_image);ImageView. Setbackgroundresource(Image[i]);Set TextView TextView TextView = (TextView) view. Findviewbyid(R. ID. Tab_title);TextView. SetText(Title[i]);Set Jump activity Intent Intent = new Intent (this, activitys[i]);Load View object and set jump activity tabspec spec = Tabhost. Newtabspec(Title[i]). Setindicator(view). SetContent(Intent);Add to Tab Tabhost. AddTab(spec);}//Tabhost. Setcurrenttabbytag("Music"); Sets the label that is displayed by default on first open, which is the same as the Tabhost.newtabspec ("music") parameterTabhost. Setcurrenttab(0);//sets the label that is displayed by default when first opened, and the parameter represents the order in which it was added to the label, starting at 0}
Well, then look at the whole code of our class.
Packagecom. SDU. Activities;Importcom. SDU. Androidmarket. R;Import Android. App. Tabactivity;Import Android. Content. Intent;Import Android. Content. Res. Resources;Import Android. Content. Res. Resources. Theme;Import Android. OS. Bundle;Import Android. View. View;Import Android. View. Window;Import Android. Widgets. ImageView;Import Android. Widgets. Tabhost;Import Android. Widgets. Tabhost. Tabspec;Import Android. Widgets. TextView;public class Markettab extends Tabactivity {private Tabhost Tabhost;Resource file private Class activitys[] = {homeactivity. Class, sortactivity. Class, hotactivity. Class, recommandactivity. Class, themeactivity. Class};//Jump ActivityPrivate String title[] = {"Home","category","Ranking","Recommended","Subject"};//The title of the settings menuprivate int image[] = {R. drawable. Tab_home, R. drawable. Tab_sort, R. drawable. Tab_hot, R. drawable. Tab_rec, R. drawable. Tab_sort};//Settings menu@Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super. OnCreate(savedinstancestate);Requestwindowfeature (Window. FEATURE_no_title);Setcontentview (R. Layout. Main_tab);InitTab ();} private void InitTab () {Resources resources = getresources ();Tabhost = (tabhost) Findviewbyid (Android. R. ID. Tabhost);Tabhost. Setup(This. Getlocalactivitymanager());Create a label for (int i =0; i < activitys.length; i++) {Instantiate a view as the tab label Layout view view = view. Inflate(This, R. Layout. Tab_layout, NULL);Set ImageView ImageView ImageView = (ImageView) view. Findviewbyid(R. ID. Tab_image);ImageView. Setbackgroundresource(Image[i]);Set TextView TextView TextView = (TextView) view. Findviewbyid(R. ID. Tab_title);TextView. SetText(Title[i]);Set Jump activity Intent Intent = new Intent (this, activitys[i]);Load View object and set jump activity tabspec spec = Tabhost. Newtabspec(Title[i]). Setindicator(view). SetContent(Intent);Add to Tab Tabhost. AddTab(spec);}//Tabhost. Setcurrenttabbytag("Music"); Sets the label that is displayed by default on first open, which is the same as the Tabhost.newtabspec ("music") parameterTabhost. Setcurrenttab(0);//sets the label that is displayed by default when first opened, and the parameter represents the order in which it was added to the label, starting at 0} }
Article source
Copyright NOTICE: Hello, reprint please leave my blog address, thank you
Android Market-Frame Building 3