当前位置: 代码迷 >> Android >> 如何在Android中切换布局
  详细解决方案

如何在Android中切换布局

热度:15   发布时间:2023-08-04 10:49:44.0

我对Android有点陌生,我正在制作一种类似于Instagram的界面。 我制作了一个布局文件,但不确定是否要正确执行。 首先,谁能告诉我我所拥有的是否正确? 其次,如何更改布局,但在所有布局中仍然有底部按钮。

这是我的主要布局:

<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/theContent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:orientation="vertical">

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:divider="@null" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/theNav"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button1"
            android:id="@+id/button1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button2"
            android:id="@+id/button2"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button3"
            android:id="@+id/button3" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button4"
            android:id="@+id/button4" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button5"
            android:id="@+id/button5" />

    </LinearLayout>

</LinearLayout>

我的意思是,如何在单击按钮时将ListView换成另一个布局?

几种方法可以做到这一点:

  1. 在xml中具有多个布局,然后更改可见性(我不建议常规使用)

  2. 添加删除的布局和视图(同样可以通过这种方式,但不建议这样做)。

  3. 片段-添加,删除已实现每种布局的片段(推荐方式)。有关片段的更多信息,请参见

您可以使用ListView使用片段,然后使用其他布局使用多个不同的片段(每个片段可以具有单独的xml布局文件)。 然后单击按钮可以添加,替换,删除片段。 这是有关的更多信息。

利用包含FragmentsViewPager ,以下是您视图的代码,但是应该在代码中实例化片段,每个片段都有自己的视图。

在这里阅读:

<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!--This is the pages container-->
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/theNav"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button1"
            android:id="@+id/button1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button2"
            android:id="@+id/button2"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button3"
            android:id="@+id/button3" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button4"
            android:id="@+id/button4" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:src="@drawable/button5"
            android:id="@+id/button5" />

    </LinearLayout>

</LinearLayout>
  相关解决方案