Android 分页控件制成底部菜单.

fmms 12年前
     <p>其实Android 中的底部菜单, 可以用分页控件很好的实现。   我们先将自定义分页控件做好, 就可以做到顶底两个位置的菜单了。</p>    <p>TabHost只是作为一个容器来存放一些Activity, 所以需要自己另外创建几个新的Activity, 然后由主TabHost加载。</p>    <p><a href="https://simg.open-open.com/show/13f02689fa90c251c8b33d9ad9991649.jpg" target="_blank"><img style="cursor:pointer;" alt="Android 分页控件制成底部菜单." src="https://simg.open-open.com/show/13f02689fa90c251c8b33d9ad9991649.jpg" width="326" height="489" /></a></p>    <p>tab_style.xml   </p>    <p>是每个Tab的自定义样式</p>    <pre class="brush:xml; toolbar: true; auto-links: false;">//分页控件样式 <?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:paddingLeft="5dip"  android:paddingRight="5dip"  android:paddingTop="5dip"  android:background="@drawable/tab_bg" ; >    <FrameLayout    android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:layout_weight="0.6"   >   <TextView     android:id="@+id/tab_label" ;   android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center"    android:background="@drawable/tab_title_selector" ;   android:textColor="#FFFFFF"    android:textStyle="bold"   />  </FrameLayout>  </LinearLayout></pre>main_tab.xml   是主TabHost布局文件    <pre class="brush:xml; toolbar: true; auto-links: false;">//TabHost布局 <?xml version="1.0" encoding="UTF-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  android:id="<a href="http://my.oschina.net/asia" class="referer" target="_blank">@android</a> :id/tabhost"   android:layout_width="fill_parent"  android:layout_height="fill_parent"  >     //必须包含下列三个View  <LinearLayout   android:orientation="vertical"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   >  <FrameLayout     android:gravity="center"     android:id="<a href="http://my.oschina.net/asia" class="referer" target="_blank">@android</a> :id/tabcontent"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_weight="1.0"   />      //TabWidget位置在FrameLayout之下则显示在低部, 在之上则显示在顶部   <TabWidget     android:id="<a href="http://my.oschina.net/asia" class="referer" target="_blank">@android</a> :id/tabs"    android:layout_height="wrap_content"    android:layout_width="fill_parent"    android:layout_weight="0.0"    />     </LinearLayout>    </TabHost></pre>    <p></p>    <p>tab_title_selector.xml</p>    <p>是Tab中TextView的按下背景</p>    <pre class="brush:xml; toolbar: true; auto-links: false;">//选择器,指示Text按下后的背景 <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">  <item   android:state_focused="true"   android:drawable="@drawable/tab_btn_bg_d" ;  />  <item   android:state_selected="true"   android:drawable="@drawable/tab_btn_bg_d" ;  />  <item   android:state_pressed="true"   android:drawable="@drawable/tab_btn_bg_d" ;  /> </selector></pre>    <p></p>    <p>Activity类</p>    <p>另外还需要几个Activity类, 普通的Activity类即可, 在此不显示。</p>    <pre class="brush:xml; toolbar: true; auto-links: false;">public class TabTest extends TabActivity {  private TabWidget mTabWidget;  private TabHost mTabHost;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState)  {   super.onCreate(savedInstanceState);   setContentView(R.layout.main_tabs);      mTabHost = getTabHost();      //将要显示的Activity载入TabHost控件   //要显示的Activity由自己自由创建   setTabIndicator("one", 1, new Intent(this, OneActivity.class));   setTabIndicator("Two", 2, new Intent(this, TwoActivity.class));   setTabIndicator("Three", 3, new Intent(this, OneActivity.class));   setTabIndicator("Four", 4, new Intent(this, TwoActivity.class));  }    private void setTabIndicator(String title, int nId, Intent intent)  {   //使用指定Tab样式   View view = LayoutInflater.from(this.mTabHost.getContext())      .inflate(R.layout.tab_style, null);      TextView text  = (TextView)view.findViewById(R.id.tab_label);   String strId  = String.valueOf(nId);      text.setText(title);      //创建一个新Tab   TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(strId)       .setIndicator(view).setContent(intent);   //加载新Tab   mTabHost.addTab(localTabSpec);  } }</pre>文章出处:    <a href="/misc/goto?guid=4959499234813838935" rel="nofollow">http://blog.csdn.net/knowheart/article/details/7049596</a>    <p></p>    <p></p>    <p></p>