【问题标题】:How to properly inflate a layout with a nested ViewFlipper?如何使用嵌套的 ViewFlipper 正确扩展布局?
【发布时间】:2013-04-18 02:42:13
【问题描述】:

我曾经有一个简单的 main.xml 布局,只有 2 个视图通过 ViewFlipper 包装器翻转。它工作(仍然工作)很好,使用以下代码:

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

我现在想在原始视图上添加 2 个按钮,其方式类似于 this

<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。

我错过了什么?我做错了什么?

【问题讨论】:

    标签: android android-linearlayout viewflipper findviewbyid


    【解决方案1】:

    尝试 project->clean(如果您使用 Eclipse)并确保您正在编辑正确的 main.xml 文件。您的代码有效,CompositeLayout 和 ViewFlipper 都不重要,按钮已绘制。

    如果您确定没有丢失任何内容并且按钮仍未绘制,请尝试将 android:layout_weight 添加到新的 LinearLayout(包含按钮)中。 (对于我的 Galaxy Nexus,没有重量一切正常,但由于 android 设备碎片可能会出现问题)

    【讨论】:

    • 谢谢。我做了无数次Project &gt; Clean,所以不是这样。但是...我刚刚发现我错误地将fill_parent 用于按钮而不是android:layout_height 中的wrap_content。由于一个愚蠢的错字而损失了这么多小时......
    猜你喜欢
    • 2011-09-14
    • 2020-10-19
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2013-12-07
    相关资源
    最近更新 更多