java - 我可以在 ViewFlipper 中设置 contentView 吗?

标签 java android xml android-linearlayout viewflipper

我有一个包含布局的 ViewFlipper。

有没有办法可以连接一个类来单独管理每个布局?

有点像 setContentView(R.layout.main); 但我不确定如何在代码中引用 3 种不同的布局。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
          <ListView android:layout_width="wrap_content" android:id="@+id/ListView" android:layout_height="220dp"></ListView>
          <ViewFlipper android:id="@+id/ViewFlipper" android:layout_width="fill_parent" android:layout_height="fill_parent">
                <LinearLayout android:id="@+id/LinearLayoutST" android:layout_width="fill_parent" android:layout_height="fill_parent">
                      <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon"></ImageView>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayoutChart" android:layout_height="fill_parent" android:layout_width="fill_parent">
                      <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 2"></TextView>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayoutDetails" android:layout_height="fill_parent" android:layout_width="fill_parent">
                      <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 3"></TextView>
                </LinearLayout>
          </ViewFlipper>
    </LinearLayout>

最佳答案

您可以通过两种方式执行此操作:

  1. 将相应的类实例附加到要操作的 View 或布局。如果您只想使用常规调用、设置 OnClickListeners 等,这就足够了:

    LinearLayoutlayoutChart = (LinearLayout)findViewById(R.id.LinearLayoutChart);

  2. 如果您需要覆盖默认类行为(即子类),请创建一个扩展类:

    公共(public)类 LinearLayoutChart 扩展 LinearLayout { ...}

确保实现所有父构造函数(即使只是传递给调用 super...),尤其是那些采用属性的构造函数。

在扩展类中实现您的自定义代码。

然后,编辑您的布局 xml,并将 LinearLayout 标记和结束标记替换为您的特殊类的完全限定类名(包括其包名称)

<com.mycompany.mypackage.LinearLayoutChart>
<!-- layout definition as before -->
</com.mycompany.mypackage.LinearLayoutChart>

这将使布局充气器实例化您的类而不是库存类,因此您可以覆盖 protected 方法等。

关于java - 我可以在 ViewFlipper 中设置 contentView 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3622849/

相关文章:

java - BigDecimal 行为怪异

java - 如何计算 JTextArea 中的行数,包括由换行引起的行数?

android - OpenGL ES 2.0 相机问题

java - 以编程方式循环从上到下移动 ImageView,在屏幕外开始和结束

c# - 从流中读取 XML

android - 将数据从 Microsoft SQL Server 数据库解析到 android 应用程序

java - 使用 jax-ws 注释将 xml 命名空间声明移动到根元素

通过 Windows 批处理文件执行 javac 和 java 命令

java - 如何从 selectonemenu JSF 获取项目值

android - 在不使用 Google API 的情况下在 Android 上将印地语音译为英语