如何使用嵌套的ViewFlipper正确膨胀布局?(How to properly inflate a layout with a nested ViewFlipper?)

编程入门 行业动态 更新时间:2024-10-14 08:26:54
如何使用嵌套的ViewFlipper正确膨胀布局?(How to properly inflate a layout with a nested ViewFlipper?)

我曾经有一个简单的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个按钮,其方式类似于:

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

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

I used to have a simple main.xml layout that had only 2 views flipped via ViewFlipper wrapper. It worked (still works) great, using the following code:

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

I now want to add 2 buttons on top of the original views, in a fashion similar to 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>

My problem is I intuitively modified the original code to by inserting a findViewById for the composite layout:

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);

But it displays exactly the same as before! As if I never added extra linearLayout02 containing the buttons.

What am I missing? What am I doing wrong?

最满意答案

尝试使用project-> clean(如果使用Eclipse)并确保您正在编辑正确的main.xml文件。 你的代码可以工作,无论是CompositeLayout还是ViewFlipper都不重要,按钮被绘制。

如果您确定没有遗漏任何东西,并且按钮仍然没有绘制,那么尝试将android:layout_weight添加到新的LinearLayout(包含按钮)中。 (对于我的Galaxy Nexus,一切都没问题,但由于Android设备碎片问题可能会出现)

Try project->clean (If you use Eclipse) and insure that you are editing right main.xml file. Your code works, neither CompositeLayout nor ViewFlipper matters, buttons are drawn.

If you're sure that nothing is missing and buttons still not drawn then try to add android:layout_weight into your new LinearLayout (containing buttons). (For my Galaxy Nexus everything is ok without weight but problem may appear because of android device fragmentation)

更多推荐

本文发布于:2023-07-14 14:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   如何使用   布局   正确   layout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!