我正在尝试编写一个每周日历,参考了以下链接的示例:
如何为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
是在一个 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
。
我做错了什么?