一菩佑、
Android studio創(chuàng)建好工程之后
首先主布局:
采用線性布局
//用來裝碎片的
<FrameLayout
android:id="@+id/maincontent"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
//分割顯示框與導(dǎo)航欄
<View android:layout_width="match_parent"
android:layout_height="0.5dp"
android:alpha="1"
android:background="@android:color/darker_gray"
/>
//用fragmentTabHost來創(chuàng)建底部的導(dǎo)航欄,注意:這里的id是固定的满力,不可以隨意亂改
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="60dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
>
</FrameLayout>
</android.support.v4.app.FragmentTabHost>
二冒窍、在創(chuàng)建要用的碎片以及碎片的xml(很簡單,Android studio會(huì)自動(dòng)填寫代碼,這里不做展示):
三榛泛、具體化導(dǎo)航欄的設(shè)置:在layout中建立一個(gè)xml來細(xì)化
<ImageView
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="fitStart"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@android:color/darker_gray"
/>
四、mainactivity(這種方法以后多加個(gè)碎片時(shí)非常方便添加):
//把要用的
private FragmentTabHost fragmentTabHost;
private String texts[] = {” 填導(dǎo)航欄中的名稱“};
private int imageButton[] = {R.你的按鈕圖片};
private Class fragmentArray[]={你的碎片.class};
//綁定導(dǎo)航欄
fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(),
R.id.maincontent);
//依次添加碎片并關(guān)聯(lián)
for (int i = 0; i < texts.length; i++){
TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
fragmentTabHost.addTab(spec, fragmentArray[i], null); fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.timg);
}
private View getView(int i) {
//加入導(dǎo)航欄具體細(xì)節(jié)
View view=View.inflate(MainActivity.this, R.layout.tabcontent, null);
ImageView imageView=(ImageView) view.findViewById(R.id.image);
TextView textView=(TextView) view.findViewById(R.id.text);
imageView.setImageResource(imageButton[i]);
textView.setText(texts[i]);
return view;
}
}
五噩斟、可以在這個(gè)基礎(chǔ)上將各個(gè)碎片精細(xì)曹锨。