3

我正在尝试按照发布的示例编写每周日历:
如何为 Android Honeycomb 应用程序创建每周日历视图?
我已在 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是在 a 内RelativeLayout,它们都在 a 内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.LayoutParamsLinearLayout.LayoutParams.

我究竟做错了什么?

4

1 回答 1

5

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

如果 a 被 theLinearLayout包裹,则RelativeLayoutLayoutpParams的类型是RelativeLayout.LayoutParams(孩子LayoutParams从父母那里获取)。您也很可能为LayoutParams您在代码中使用的类导入了错误的类。

LinearLayout lay = (LinearLayout) findViewById(R.id.currentTimeMarkerLinearLayout);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)lay.getLayoutParams();
于 2012-12-06T15:41:55.690 回答