【问题标题】:Can I set a contentView inside of a ViewFlipper?我可以在 ViewFlipper 中设置 contentView 吗?
【发布时间】:2010-09-01 23:34:16
【问题描述】:

我有一个包含布局的 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>

【问题讨论】:

    标签: java android xml android-linearlayout viewflipper


    【解决方案1】:

    您可以通过两种方式做到这一点:

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

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

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

      公共类 LinearLayoutChart 扩展 LinearLayout { ...}

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

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

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

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

    这将使布局膨胀器实例化您的类而不是股票类,因此您可以覆盖受保护的方法等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2011-02-05
      • 2013-12-04
      • 1970-01-01
      • 2021-08-16
      相关资源
      最近更新 更多