android - 多次调用 setContentView()

标签 android

有没有办法在一个 Activity 期间使用不同的 id 多次调用 setContentView(id) 以呈现不同的 View ,或者我是否必须启动一个新的 Activity?

最佳答案

根据 Austyn 的评论,我确实设法在另一篇文章中找到了一些关于如何使用 ViewFlipper 来完成此操作的指南(请参阅已勾选的最佳答案 here。)

如果您不想使用 ViewFlipper,我找到了一个很好的示例,说明如何在同一 View 中切换布局 here :

XML:

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView 
        android:src="@drawable/icon"
        android:scaleType="fitCenter"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>
    <TextView
        android:text="Learn-Android.com"
        android:textSize="24sp"
        android:textColor="#000000"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center"/>
</FrameLayout>

代码:

private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}

private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}

关于android - 多次调用 setContentView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4018772/

相关文章:

android - 验证 Android 中编辑文本的背景是否相同

android - 无法在 AVD 模拟器上安装 apk 文件

java - 从我的 android 应用程序打开 youtube 应用程序。如何传递id?

android - 如何删除约束布局中 View 之间的额外空间?

android - javax.net.ssl.SSLProtocolException : SSL handshake aborted: ssl=0x7fa6b56a00: Failure in SSL library, 通常是协议(protocol)错误

android - 在 fresco lib 中下载图像

java - 如何检测Android中点击图像的区域

java - 获取动态链接,而不是深层链接 - Android 上的 Firebase 动态链接

java - 错误没有找到处理 Intent act=android.media.action.IMAGE_CAPTURE 的 Activity

android - 尝试在空对象引用上调用接口(interface)方法 com.google.firebase.auth.FirebaseAuth$AuthStateListener.onAuthStateChanged'