簡體   English   中英

Android-設置布局參數時視圖未顯示

[英]Android - View not showing when setting Layout Params

在我的Android應用程序中,我正在定義以下布局,這些布局工作正常。

     <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black">
        <com.test.zoom.ImageZoomView
            android:id="@+id/zoomview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ZoomControls
            android:id="@+id/zoomControls"
            android:layout_width="130dp"
            android:layout_height="50dp"
            android:layout_gravity="bottom|right"/>
    </FrameLayout>

但是,當在FrameLayout上設置布局參數時,ZoomControls消失了。 你有暗示我在做什么錯嗎?

    ViewTreeObserver viewTreeObserver = frame.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                frame.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                frame.setLayoutParams(new LinearLayout.LayoutParams(frame.getMeasuredWidth(), frame.getMeasuredWidth()));
            }
        });
    }

您的ZoomControls是FrameLayout的子級。 因此,如果刪除FrameLayout,其所有子項(com.test.zoom.ImageZoomView&ZoomControls)也將被刪除。
嘗試使FrameControl獨立於ZoomControl,如下所示:

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">
    <com.test.zoom.ImageZoomView
        android:id="@+id/zoomview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

<ZoomControls
        android:id="@+id/zoomControls"
        android:layout_width="130dp"
        android:layout_height="50dp"
        android:layout_gravity="bottom|right"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM