如何正确地使用嵌套的ViewFlipper填充布局?

4

我曾经有一个简单的main.xml布局,只有两个视图通过ViewFlipper包装翻转。它运行良好(仍然可以使用),使用以下代码:

setContentView(R.layout.main);
mTV1 = (TextView) findViewById(R.id.textview01);
mTV2 = (TextView) findViewById(R.id.textview02);
mViewFlipper = (ViewFlipper)findViewById(R.id.flipper01);

我现在希望在原视图的顶部添加两个按钮,类似于这样的方式:

<LinearLayout
 android:id="@+id/linearLayout01" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" 
 xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
 android:id="@+id/linearLayout02" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">
    <Button android:id="@+id/button01" android:layout_height="wrap_content" android:text="Button 1" android:layout_width="0dip" android:layout_weight="1"></Button>
    <Button android:id="@+id/button02" android:layout_height="wrap_content" android:text="Button 2" android:layout_width="0dip" android:layout_weight="1"></Button>
</LinearLayout>

<RelativeLayout
 android:id="@+id/relativeLayout01" 
 android:layout_width="fill_parent" 
 android:layout_height="0dp"
 android:layout_weight="1">
    <ViewFlipper
        android:id="@+id/flipper01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView
        android:id="@+id/textview01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        />  
        <TextView
        android:id="@+id/textview02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text2"
        />  

    </ViewFlipper>
    </RelativeLayout>
</LinearLayout>

我的问题在于,我直觉地修改了原始代码,通过插入一个 findViewById 来获取复合布局:
setContentView(R.layout.main);
mCompositeLayout = (LinearLayout) findViewById(R.id.linearLayout02);
mTV1 = (TextView) findViewById(R.id.textview01);
mTV2 = (TextView) findViewById(R.id.textview02);
mViewFlipper = (ViewFlipper)findViewById(R.id.flipper01);

但是它显示的和之前完全一样!好像我从未添加过包含按钮的额外线性布局LinearLayout02。
我错过了什么?我做错了什么?
1个回答

1
尝试使用project->clean(如果您使用的是Eclipse),确保您正在编辑正确的main.xml文件。您的代码有效,无论是CompositeLayout还是ViewFlipper都无关紧要,按钮已绘制。
如果您确定没有遗漏任何内容并且仍未绘制按钮,则尝试将android:layout_weight添加到新的LinearLayout中(其中包含按钮)。 (对于我的Galaxy Nexus,没有权重一切正常,但由于Android设备的碎片化问题可能会出现问题)

谢谢。我已经无数次执行了“项目>清理”,所以那不是问题所在。但是...我刚刚发现我错误地将按钮的android:layout_height设置为fill_parent而不是wrap_content。由于一个愚蠢的打字错误,浪费了这么多时间... - scatmoi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接