0

次の線形レイアウトがあり、それを子として Horizo​​ntalScrollView に設定しています

ファイル menu.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu"
android:orientation="vertical" android:background="#2f4f4f" android:layout_width="wrap_content" android:layout_height="fill_parent">

  <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:background="#2f4f4f" android:cacheColorHint="#2f4f4f" android:scrollbars="none">
  </ListView>
</LinearLayout>

ファイル: scrollview.xml

 <?xml version="1.0" encoding="utf-8"?>
 <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00ffffff" android:padding="0px"
android:layout_margin="0px" android:fadingEdge="none" android:fadingEdgeLength="0px" android:scrollbars="none">
    <LinearLayout android:id="@+id/top" android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:orientation="horizontal" android:background="#ffffffff" android:padding="0px" android:layout_margin="0px">
    </LinearLayout>
</HorizontalScrollView>

プログラムで線形レイアウトの幅を設定しようとしていますが、ClassCastException が発生しています。(LinearLayout$LayputParams は FrameLayout$LayputParams にキャストできません)

LayoutInflater inflater = LayoutInflater.from(this);
scrollView = inflater.inflate(R.layout.scrollview_xml, null);
setContentView(scrollView);
View menu = inflater.inflate(R.layout.menu_xml, null);
scrollView.addView(menu);
scrollView.getChildAt(0).setLayoutParams(new ViewGroup.LayoutParams(100,100));

これを解決するには?

4

2 に答える 2

1
  1. scrollViewはFrameLayoutを拡張するため、その子の layoutParams を FrameLayout の layoutParams を持つように設定する必要があります。

  2. 間違ったレイアウトにlayoutParamsを設定したために例外があると表示されているので、正しいものを使用してください:

    scrollView.getChildAt(0).setLayoutParams(新しいFrameLayout .LayoutParams(100,100));

  3. 一般に、レイアウトのタイプについて疑問がある場合は、次を使用します。

    scrollView.getChildAt(0).setLayoutParams(新しいViewGroup .LayoutParams(100,100));

于 2012-12-29T08:27:50.983 に答える
0

直接使用できるのに、layoutParamsを型キャストする理由

scrollView.getChildAt(0).setLayoutParams(new LayoutParams(100,100));
于 2012-12-29T08:32:48.467 に答える