簡體   English   中英

避免在更改邊距時調整布局

[英]Avoid layout to be resized on margin changed

我正在以編程方式更改框架布局內的布局邊距。 我的目標是制作一個像 Facebook 應用程序一樣的滑動視圖。 是否可以避免調整此布局的大小? 這是我的布局移動:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#00a2ff"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/ivGPSSearching"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"
            android:src="@drawable/actionbar_gps_searching" />
    </LinearLayout>

    <ImageView
        android:id="@+id/ivStarsFilter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:src="@drawable/actionbar_star" />
</LinearLayout>

我不希望調整左側 LinearLayout 的大小。 我希望你會明白我想要什么:)

謝謝

為了實現類似的效果,我們擴展了 Horizo​​ntalScrollView - 只是覆蓋 onInterceptTouchEvent 和 onTouchEvent 以始終​​返回 false。 然后你只需要把你的菜單放在左邊,內容放在右邊(內容必須與屏幕寬度匹配)。

最后設置初始 Horizo​​ntalScrollView 滾動並綁定到按鈕單擊事件以更改滾動位置(horizo​​ntalScrollView.scrollTo 或 horizo​​ntalScrollView.smoothScrollTo)。

我希望這有幫助。

以下是使用 TranslateAnimation 並為動畫設置偵聽器的示例代碼,並在

   @Override
    public void onAnimationEnd(Animation animation) 
    {

    //Just set the layout params for your view (layout) which is animated,
    //to make your view shift to  the new position

     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutWidth, LayoutHeight);

     params.leftMargin = 100; //for example you want to shift 100 px from left

     params.rightMargin = -Layoutwidth; //this will avoid your view 
     //from shrinking and make it as wide as its width was, and - negative value will 
     //move it towards right

    otherLayout.setLayoutParams(params);

    }

我希望它能讓你們清楚如何在動畫后移動你的視圖

在過去的幾個小時里,我也一直在為此撓頭。 事實證明,如果您更改兩個相反的邊距,則 ReleativeLayout 內的視圖將不會調整大小或四處移動:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.setMargins(0, - yOffset, Integer.MIN_VALUE, yOffset);

這很瘋狂,但它有效......

暫無
暫無

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

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