java - android中的一个简单的TabHost

标签 java android android-tabhost android-button

我在Android中创建了一个简单的tab项目

MainActivity.java

public class MainActivity extends TabActivity {

    // TabSpec Names
        private static final String TAB1 = "Tab1";
        private static final String TAB2 = "Tab2";
        private static final String TAB3 = "Tab3";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            TabHost tabHost = getTabHost();

            // Inbox Tab
            TabSpec inboxSpec = tabHost.newTabSpec(TAB1);
            Intent inboxIntent = new Intent(this, Tab1.class);
            inboxSpec.setIndicator(TAB1);
            // Tab Content
            inboxSpec.setContent(inboxIntent);

            // Outbox Tab
            TabSpec PriceSpec = tabHost.newTabSpec(TAB2);
            Intent PriceIntent = new Intent(this, Tab2.class);
            PriceSpec .setIndicator(TAB2);
            PriceSpec.setContent(PriceIntent);

            // Profile Tab
            TabSpec DistanceSpec = tabHost.newTabSpec(TAB3);
            Intent DistanceIntent = new Intent(this, Tab3.class);
            DistanceSpec .setIndicator(TAB3); 
            DistanceSpec.setContent(DistanceIntent);

            // Adding all TabSpec to TabHost
            tabHost.addTab(inboxSpec); 
            tabHost.addTab(PriceSpec); 
            tabHost.addTab(DistanceSpec); 

            //Set the current value tab to default first tab
            tabHost.setCurrentTab(0);

            //Setting custom height for the tabs
            final int height = 45;
            tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
            tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
            tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = height;
        }

}

ma​​in.xml

<?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="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>

输出::

enter image description here


我如何在选项卡顶部制作一组水平的 5 个按钮以获得下图中的类似内容

enter image description here


我需要在 main.xml 中进行哪些代码更改?

最佳答案

<?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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <include layout="@layout/screen_topbar" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@color/white" >
            </TabWidget>
        </FrameLayout>
    </LinearLayout>

</TabHost>

和TopBar布局-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonlayout"
    android:layout_width="fill_parent"
    android:layout_height="53dp"
    android:gravity="top"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>

只要试试这个就可以了..

关于java - android中的一个简单的TabHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18973132/

相关文章:

Java 对象缓存,从文件读取还是从远程机器读取哪个更快?

java - Java 中的每个自定义异常都需要自己的类吗?

Android:是否可以在自定义搜索建议中插入永久选项?

安卓异常 : Did you forget to call 'public void setup (LocalActivityManager activityGroup)'

android - Android 中类似 iPhone 的标签栏?

java - boolean 方法不返回 false

java - 用 Java 检测歌曲

android - 是否可以通过适用于 GLES 的 Java API 在 Android 上使用像素图?

带有 ListView CHOICE_MODE_MULTIPLE 的 Android onSelectedItemListener

android - 在 android 中调整 tabhost 中的多个选项卡?