如何在Android中切换布局 [英] How to switch between layouts in android
问题描述
我对Android有点陌生,我正在制作一种类似于Instagram的界面.我制作了一个布局文件,但不确定是否要正确执行.首先,谁能告诉我我所拥有的是否正确?其次,如何更改布局,但在所有布局中仍然有底部按钮.
I am a bit new to Android and I am making an interface sort of like Instagram has theirs. I made a layout file and I am not sure if I am doing it a proper way. First can anyone tell me if what I have is correct? And second, how can I change the layout but still have the bottom buttons there in all of the layouts.
这是我的主要布局:
<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换成另一种布局?
what I am saying is, how can I swap out that ListView for another layout on a button click?
推荐答案
几种方法可以做到:
-
在xml中具有多个布局,然后更改可见性(我不建议常规使用)
Have multiple layouts in xml and then change visibility (I do not recommend for regular use)
添加删除版面&观看次数(也可以通过这种方式,但不建议这样做).
Add remove layouts & views (again possible this way, but wouldn't recommend).
片段-添加,删除已实现每种布局的片段(推荐方式)有关片段的更多信息,请参见:片段
Fragments - add, remove fragments which have implementation of each layout (recommended way) More about fragments here: fragments
您可以使用ListView创建片段,然后使用其他布局创建多个不同的片段(每个片段可以具有单独的xml布局文件).然后在按钮上单击,您可以添加,替换,删除片段.这是有关片段交易的更多信息.
You can have fragment with ListView, then number of different fragments with other layouts (each fragment can have separate xml layout file). And then on button click you can add, replace, remove fragments. Here's more info about fragment transaction.
这篇关于如何在Android中切换布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!