Android 以编程方式设置SlidengAppAnnelLayout的面板高度

Android 以编程方式设置SlidengAppAnnelLayout的面板高度,android,slidingpanelayout,Android,Slidingpanelayout,我已经在我的应用程序中实现了SlidengAppAnnelLayout,效果很好。现在我想在我的活动创建后动态设置面板高度,现在我在xml中这样做 <com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto" android:id="@+id/sliding_layout" android:layout_width="m

我已经在我的应用程序中实现了SlidengAppAnnelLayout,效果很好。现在我想在我的活动创建后动态设置面板高度,现在我在xml中这样做

<com.sothree.slidinguppanel.SlidingUpPanelLayout
    xmlns:sothree="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    sothree:umanoPanelHeight="68dp"
    sothree:umanoShadowHeight="4dp"
    sothree:umanoParalaxOffset="100dp"
    sothree:umanoDragView="@+id/dragView"
    sothree:umanoOverlay="true">

    <!-- MAIN CONTENT -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Main Content"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="true"
            android:textSize="16sp" />
    </FrameLayout>

    <!-- SLIDING LAYOUT -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical"
        android:clickable="true"
        android:focusable="false"
        android:id="@+id/dragView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="14sp"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"/>

            <Button
                android:id="@+id/follow"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textSize="14sp"
                android:gravity="center_vertical|right"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"/>

        </LinearLayout>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitStart"
            android:src="@drawable/graphic" >
        </ImageView>
    </LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
但是当我这样做的时候,我会崩溃

04-23 20:09:24.010:E/AndroidRuntime(13344):原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int-android.view.view.getLeft()”

 mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
 mLayout.setPanelSlideListener(new PanelSlideListener() {

    @Override
    public void onPanelSlide(View panel, float slideOffset) {

        Log.i(TAG, "onPanelSlide, offset " + slideOffset);
    }

    @Override
    public void onPanelExpanded(View panel) {

        Log.i(TAG, "onPanelExpanded");
    }

    @Override
    public void onPanelCollapsed(View panel) {

        Log.i(TAG, "onPanelCollapsed");
    }

    @Override
    public void onPanelAnchored(View panel) {

        Log.i(TAG, "onPanelAnchored");
    }

    @Override
    public void onPanelHidden(View panel) {

        Log.i(TAG, "onPanelHidden");
    }
});
mLayout.setPanelHeight(Utils.getPixels(80, getResources()));
因为在SlidengAppAnnelayout.java的smoothSlideTo方法中,mslidableView
为空

提前感谢。

请尝试以下操作:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
mLayout.setPanelHeight(height/2);

也可以看看这篇文章:

之所以会这样,是因为你们并没有给版面足够的时间来充气。同样的事情也发生在我身上,我尝试了以下方法:

final int someWaitInterval = 20;
Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                mLayout.setPanelHeight(params.height);
            } catch (Exception e2) {
                new Handler().postDelayed(this, someWaitInterval);
            }
        }};
new Handler().postDelayed(r, someWaitInterval);

通过这种方式,它将调用自身,直到它实际更改面板高度。

您需要在onPredrawListener中设置高度。

上面的代码不是在onCreate中工作,而是在onWindowFocusChange中工作。对我来说,这有什么原因吗?