2016-07-27 5 views
0

問題があります。私はmarginally paramsをプログラム的に設定したいが、うまくいかない。 私はこのXMLスタイルシートがあります。LinearLayoutはCoordinatorLayoutにキャストできません

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 


</android.support.design.widget.AppBarLayout> 


<ImageView 
    android:layout_width="1440px" 
    android:layout_height="1000px" 
    android:id="@+id/post_view_photo" 
    android:transitionName="@string/transition_image"/> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical|start" 
    android:layout_margin="@dimen/fab_margin" 
    app:layout_anchor="@+id/post_view_photo" 
    android:src="@android:drawable/stat_notify_chat" 
    app:layout_anchorGravity="bottom|end" /> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    app:layout_anchor="@+id/post_view_photo" 
    app:layout_anchorGravity="bottom|start"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/text_bar" 
     android:background="@color/colorPrimary"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/username_text" 
      android:layout_gravity="center_vertical|start" 
      app:layout_anchor="@+id/post_view_photo" 
      app:layout_anchorGravity="bottom|start" 
      android:textSize="25dp" 
      android:transitionName="@string/transition_username"/> 
    </LinearLayout> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_marginTop="808px" 
    android:id="@+id/view_bottom_layout"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <ImageView 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:id="@+id/post_user_image" 
       android:src="@drawable/back" 
       android:paddingTop="10px"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/post_view_maintext"/> 
     </LinearLayout> 

    </LinearLayout> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="#414141"/> 
</LinearLayout> 

を、私はプログラム的にマージンを設定します。

java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams 

そして私は、私の活動でこれを行う:しかし、私はこのエラーを取得する

LinearLayout bottomLayout = (LinearLayout)findViewById(R.id.view_bottom_layout); 
         LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) bottomLayout.getLayoutParams(); 
         params.setMargins(0,imageView.getHeight(),0,0); 
         bottomLayout.setLayoutParams(params); 

が、私はこれを修正するために何ができますか?私は私を助けたものは何も見つかりませんでした。ありがとうございました!

答えて

3

LayoutParamsは、見つけたレイアウトの親レイアウトに常に関連付けられています。

質問のレイアウトファイルがクリップされているので、view_bottom_layoutがCoordinateLayout内にあるという出力に基づいていると仮定しています。

もしそうなら、あなたはCoordinateLayoutののparamsを必要とし、それらを設定します。

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) bottomLayout.getLayoutParams(); 
        params.setMargins(0,imageView.getHeight(),0,0); 
        bottomLayout.setLayoutParams(params); 
1

あなたが実際にその親にgetLayoutParams()をキャストする必要があり、あなたのケースでは、それはCoordinator Layoutです。

関連する問題