Android 底部导航栏 – 多 Activity(TabActivity)

Android 底部导航栏 – 多 Activity(TabActivity)

原文地址:https://blog.csdn.net/yubo_725/article/details/41899203

使用 TabHost 的方式,现在已经很少用多个 Activity 的写法了,都是一个 Activity + 多个 Fragment。

这里用了一个 TabHostActivity + 多个 Activity

 

/**
 * 这个主Activity必须继承自 TabActivity
 */
public class TabHostActivity extends TabActivity {
    // 用来构建底部的导航和导航上方的界面
    private TabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost = getTabHost();
        /**
         * 添加tab标签,
         * setIndicator(CharSequence label) 底部的文字
         * setIndicator(CharSequence label, Drawable icon) 文字 + 图片
         * // setIndicator("选项3", getResources().getDrawable(R.drawable.tab_img_selector)
         * --
         * setIndicator(View view)
         * --
         * setContent() 要显示的 Activity,没有特别的要求
         *
         */
        tabHost.addTab(tabHost.newTabSpec("home").setIndicator("Home").setContent(new Intent(this, HomeActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("chat").setIndicator("Chat").setContent(new Intent(this, ChatActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("search").setIndicator("Search").setContent(new Intent(this, SearchActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("mine").setIndicator("Mine").setContent(new Intent(this, MineActivity.class)));
    }
}

// 这个 xml 里面的id命名是固定写法

@android:id/tabhost

@android:id/tabcontent

@android:id/tabs

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TabHostActivity">

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

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</TabHost>

呈现内容的几个 Activity,这里不需要固定写法(可以自定义)

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }
}
----
public class ChatActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
    }
}
----
public class SearchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
    }
}
----
public class MineActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
    }
}

xml 布局,这里不需要固定写法(可以自定义)

activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".navigation.HomeActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="home"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="320dp" />
</LinearLayout>
----
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".navigation.ChatActivity">

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="chat"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="341dp" />
</LinearLayout>
----
activity_search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".navigation.ChatActivity">

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="chat"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="341dp" />
</LinearLayout>
----
activity_mine.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".navigation.MineActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="mine"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="341dp" />
</LinearLayout>

 

发表评论

zh_CNChinese
zh_CNChinese