在对话框内的ViewFlipper内部没有标题的FILL_PARENT

时间:2012-01-07 10:15:16

标签: android android-layout viewflipper

我正在尝试创建一个ViewFlipper,其中每个子视图都有一些最小大小(基于其内容),但每个子视图应填充外部ViewFlipper。所以我想如果我确定每个孩子FILL_PARENT的大小,它就会完全按照我的意愿行事。

然而,似乎会发生的事情是每个孩子的大小是单独计算的,然后是最大值,这是ViewFlipper的大小,但孩子的布局不会重新计算到匹配这个新的大小。奇怪的是,这似乎只有在设置FEATURE_NO_TITLE时才会发生。

以下是问题的演示。子0的大小为FILL_PARENT x FILL_PARENT,子级1的大小为100x100,因此ViewFlipper的大小正确地确定为100x100。但是,子0不会调整为100x100;实际上,它的大小为0x0(正如您在运行应用程序时看到的那样,看到红色矩形没有绿色痕迹)。但是,如果删除该行

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

然后按预期工作...... 这是怎么回事?

代码:

package com.example.flipper;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity
{
    ViewFlipper flipper;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.flipper = new ViewFlipper(this);
        flipper.setBackgroundColor(0xffff0000);

        {
            LinearLayout ll = new LinearLayout(this);
            ll.setBackgroundColor(0xff00ff00);
            flipper.addView (ll, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        }

        {
            LinearLayout ll = new LinearLayout(this);
            ll.setBackgroundColor(0xff0000ff);
            flipper.addView (ll, new ViewGroup.LayoutParams(100, 100));
        }

        setContentView(flipper);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e)
    {
        flipper.setDisplayedChild(1);
        return true;
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.flipper"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:label="@string/app_name" >
        <activity android:name="MainActivity"
                  android:theme="@android:style/Theme.Dialog"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

1 个答案:

答案 0 :(得分:1)

我似乎能用kludge解决这个问题:我将ViewFlipper包裹在LinearLayout中并将其设置为活动的视图:

LinearLayout llOuter = new LinearLayout(this);
llOuter.addView(flipper, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

setContentView(llOuter);