0

我得到了高度 0,它在 log cat 中打印。

在 MainJava 文件上:

RealViewSwitcher realViewSwitcher = (RealViewSwitcher) findViewById(R.id.horizontal_pager);
        realViewSwitcher.setOnScreenSwitchListener(onScreenSwitchListener);

RealViewSwitcher.java 文件,其中包括:@Override

protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
    Log.d("TAG", "First MeastueSpece.Exactly Out Put:"+MeasureSpec.EXACTLY);
    Log.d("TAG", "First Width Out Put:"+widthMode);
    throw new IllegalStateException("ViewSwitcher can only be used in EXACTLY mode. at Width Mode");
}

final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {

    Log.d("TAG", "First MeastueSpece.Exactly  Out Put:"+MeasureSpec.EXACTLY);
    Log.d("TAG", "First heightModeOut Put:"+heightMode);
    throw new IllegalStateException("ViewSwitcher can only be used in EXACTLY mode. at Height Mode");
}
......

在布局文件中:

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="45dp"
        android:layout_marginTop="54dp" >

        <com.test.RealViewSwitcher
            android:id="@+id/horizontal_pager"
            android:layout_width="fill_parent"
            android:layout_height="0px"
            android:orientation="vertical" 
            android:layout_weight="1" >
        </com.test.RealViewSwitcher>
    </ScrollView>

我在 Logcat 中得到了这个:

06-20 09:42:55.952: D/TAG(1329): First heightMode Out Put:0
06-20 09:42:55.962: D/TAG(1329): First MeastueSpece.Exactly Out Put:1073741824
06-20 09:42:55.962: W/Trace(1329): Unexpected value from nativeGetEnabledTags: 0
06-20 09:42:55.972: W/Trace(1329): Unexpected value from nativeGetEnabledTags: 0
06-20 09:42:55.972: D/AndroidRuntime(1329): Shutting down VM
06-20 09:42:55.984: W/dalvikvm(1329): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
06-20 09:42:56.253: E/AndroidRuntime(1329): FATAL EXCEPTION: main
06-20 09:42:56.253: E/AndroidRuntime(1329): java.lang.IllegalStateException: ViewSwitcher can only be used in EXACTLY mode. at Height Mode
4

1 回答 1

0

只需硬编码高度。换成这个,

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="45dp"
    android:layout_marginTop="54dp" >

    <com.test.RealViewSwitcher
        android:id="@+id/horizontal_pager"
        android:layout_width="fill_parent"
        android:layout_height="550dp"
        android:orientation="vertical" 
        android:layout_weight="1" >
    </com.test.RealViewSwitcher>
</ScrollView>
于 2013-08-16T06:56:20.623 回答