2017-03-08 74 views
2


我在Framelayout內部有一個Linear layout。我想要做的是基於屏幕大小以編程方式向線性佈局添加一些邊距。
無法在Framelayout內設置Linearlayout參數

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:scrollbars="none"> 
    <FrameLayout 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.example.aba.webcampstest.MainActivity" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/headerImageView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:adjustViewBounds="true" 
      android:layout_marginTop="0dp" 
      android:scaleType="centerCrop"/> 

     <LinearLayout 
      android:id="@+id/mainContainerLayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

     //oher stuffs 
     </LinearLayout> 
    </FrameLayout > 
</ScrollView> 

而且我的活動onCreate方法包含

LinearLayout containerLayout = (LinearLayout) findViewById(R.id.mainContainerLayout); 
int marginTopBottom = dpToPx((int)((dpHeight*8)/100),(int)density); //8% 
int marginLeftRight = dpToPx((int)((dpWidth*6)/100),(int)density); //6% 

LinearLayout.LayoutParams languageContainerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
languageContainerLayoutParams.setMargins(marginLeftRight,marginTopBottom,marginLeftRight,marginTopBottom); 
containerLayout.setLayoutParams(languageContainerLayoutParams); 

我開始活動

E/AndroidRuntime時出現錯誤: 致命異常:主要 過程:COM .example.aba.webcampstest,PID:11723 java.lang.ClassCastException:a ndroid.widget.LinearLayout $ LayoutParams不能投到 android.widget.FrameLayout $ LayoutParams at android.widget.FrameLayout.onMeasure(FrameLayout.java:311) at android.view.View.measure(View.java:17495 ) 在android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1779) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在android.widget.ScrollView.onMeasure(ScrollView.java:476) at android.view.View.measure(View.java:17495) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android .view.View.measure(View.java:17495 ) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 在android.view.View.measure(View.java:17495) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) 在機器人.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2548) at android.view.View.measure(View.java :17495) at android.view.V iewRootImpl.performMeasure(ViewRootImpl.java:2285) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595) 在android.view.ViewRootImpl。 doTraversal(ViewRootImpl.java:1254) at android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6637) at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:814) at android.view。 Chronographer.doCallbacks(Choreographer.java:614) at android.view.Choreographer.doFrame(Choreographer.java:584) at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:800) at android.os。 Handler.handleCallback(手在android.app.ActivityThread.main(ActivityThread。)上的android.os.Looper.loop(Looper.java:146) 上的android.os.Handler.dispatchMessage(Handler.java:95) 。 java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .run(ZygoteInit.java:1283) at com.android。internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在dalvik.system.NativeStart.main(本機方法)

可能是什麼問題呢?

回答

2

setLayoutParamsdescription

設置佈局與該視圖相關聯的參數。這些視圖的父級提供參數,指定應如何安排。 ViewGroup.LayoutParams有許多子類,它們對應ViewGroup的不同子類,它們負責安排子級。

LayoutParams類型應該是相對於你LinearLayout的容器,在這種情況下FrameLayout.LayoutParams

FrameLayout.LayoutParams languageContainerLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); 
languageContainerLayoutParams.setMargins(marginLeftRight,marginTopBottom,marginLeftRight,marginTopBottom); 
containerLayout.setLayoutParams(languageContainerLayoutParams); 
+0

你應該先設置你的父母的佈局參數,然後它的child..in你的情況下,父親是#FrameLayout視圖activity_main的 –

+0

LayoutParametres(的FrameLayout )已經在xml中設置了。就像mainContainerLayout的params一樣,但Coder想要在運行時改變它們。 – Goran

2

閱讀例外更仔細,特別是這部分:

的java.lang .ClassCastException:android.widget.LinearLayout $ LayoutParams不能轉換爲android.widget.FrameLayout $ LayoutParams

爲了說明,在設置視圖的LayoutParams時,您正在聲明視圖如何在其父元素內定位。

因此,對於此示例,您提供了使用FrameLayout.LayoutParams。在第一個註釋

更新基礎:

讓我告訴你一個例子:

LinearLayout containerLayout = (LinearLayout) 

findViewById(R.id.mainContainerLayout); 
int marginTopBottom = dpToPx((int)((dpHeight*8)/100),(int)density); //8% 
int marginLeftRight = dpToPx((int)((dpWidth*6)/100),(int)density); //6% 

FrameLayout.LayoutParams languageContainerLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); 
languageContainerLayoutParams.setMargins(marginLeftRight,marginTopBottom,marginLeftRight,marginTopBottom); 
containerLayout.setLayoutParams(languageContainerLayoutParams); 

您對設置的LinearLayout PARAMS,但佈局內的FrameLayout。所以LinearLayout的LayoutParams的類型是FrameLayout.LayoutParams

您應該使用哪種類型的LayoutParams基於您的視圖所在的父類型。

+0

但我想將佈局參數設置爲linearlayout,如前所示。不適用於框架佈局.... – Coder

+0

希望此編輯能夠消除混淆。 – Goran

0

你好@Coder請檢查該段將工作

LinearLayout containerLayout = (LinearLayout) findViewById(R.id.mainContainerLayout); 
int marginTopBottom = dpToPx((int)((dpHeight*8)/100),(int)density); //8% 
int marginLeftRight = dpToPx((int)((dpWidth*6)/100),(int)density); //6% 

ViewGroup.LayoutParams layoutParams = containerLayout.getLayoutParams(); 
((ViewGroup.MarginLayoutParams) layoutParams).topMargin=marginTopBottom; 
((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin=marginTopBottom; 
((ViewGroup.MarginLayoutParams) layoutParams).leftMargin=marginLeftRight; 
((ViewGroup.MarginLayoutParams) layoutParams).rightMargin=marginLeftRight; 
containerLayout.setLayoutParams(layoutParams); 
相關問題