Android 选项卡(TabHost)例子(3)

android:layout_width="wrap_content"
                                           

android:layout_height="wrap_content"
                                           

android:layout_gravity="center"
                                            android:src="@drawable/help"/>
                            </LinearLayout>
                    </FrameLayout>
            </RelativeLayout>
    </TabHost>


Activity文件 TabDemo.java


    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.widget.TabHost;
    import android.widget.Toast;
    import android.widget.TabHost.OnTabChangeListener;

import com.kf.samples5.R;

public class TabDemo extends Activity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);
    TabHost host = (TabHost)findViewById(R.id.tabhost);
    host.setup();

TabHost.TabSpec homeSpec = host.newTabSpec("Home");        //This

param will be used as tabId.
    homeSpec.setIndicator(null,         //This param will diplay as title.
    getResources().getDrawable(R.drawable.home_normal));
    homeSpec.setContent(R.id.tab1);
    host.addTab(homeSpec);

TabHost.TabSpec garbageSpec = host.newTabSpec("Garbage");
    garbageSpec.setIndicator(null, getResources().getDrawable

(R.drawable.garbage_normal));
    garbageSpec.setContent(R.id.tab2);
    host.addTab(garbageSpec);

TabHost.TabSpec maybeSpec = host.newTabSpec("Help");
    maybeSpec.setIndicator(null, getResources().getDrawable

(R.drawable.help_normal));
    maybeSpec.setContent(R.id.tab3);
    host.addTab(maybeSpec);

host.setOnTabChangedListener(new OnTabChangeListener() {

@Override
    public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    Toast toast = Toast.makeText(TabDemo.this, tabId, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
    toast.show();
    }
    });

//         host.setCurrentTabByTag("Home");
    Toast toast = Toast.makeText(TabDemo.this, "Home",

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwgyjf.html