类型不匹配:无法从ViewGroup.LayoutParams转换为LinearLayout.LayoutParams

时间:2012-12-06 12:54:25

标签: java android xml android-layout

我正在尝试按照发布的示例编制每周日历:
How can I create a weekly calendar view for an Android Honeycomb application?
我已经在XML文件中添加了一个时间标记行,假设它在当前时间。要做到这一点,我使用:

<LinearLayout
    android:id="@+id/currentTimeMarkerLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="120dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:padding="0dp" >

  <View
      android:layout_width="0dp"
      android:layout_height="3dp"
      android:layout_weight="1" />

  <View
      android:id="@+id/currentTimeLineView"
      android:layout_width="0dp"
      android:layout_height="1dp"
      android:layout_weight="14"
      android:background="@color/dark_blue" />
</LinearLayout>

LinearLayout位于RelativeLayout内,且ScrollView位于layout_margintTop内。 我试图通过编程“移动”这一行,通过这些行修改int dipValue = 720; // we want to convert this dip value to pix Resources r = getResources(); float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics()); LinearLayout lay =(LinearLayout) findViewById(R.id.currentTimeMarkerLinearLayout); LinearLayout.LayoutParams lp = lay.getLayoutParams(); lp.setMargins(0, (int) pix, 0, 0); lay.setLayoutParams(lp);

lp = lay.getLayoutParams()

但我在ViewGroup.LayoutParams中收到错误,告诉我它无法从LinearLayout.LayoutParams转换为{{1}}。

我做错了什么?

1 个答案:

答案 0 :(得分:4)

  

但是我在lp = lay.getLayoutParams()中收到错误告诉我它   无法从ViewGroup.LayoutParams转换为   LinearLayout.LayoutParams。

如果LinearLayoutRelativeLayout包裹,而LayoutpParams的{​​{1}}类型为RelativeLayout.LayoutParams(孩子从父母那里获取LayoutParams)。您也很可能为代码中使用的LayoutParams导入了错误的类。

LinearLayout lay = (LinearLayout) findViewById(R.id.currentTimeMarkerLinearLayout);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)lay.getLayoutParams();