我正尝试按照发布在以下位置的示例编写周历:
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
.
我做错了什么?
最佳答案
But I get an error in lp = lay.getLayoutParams() telling me that it can't convert from ViewGroup.LayoutParams to LinearLayout.LayoutParams.
如果 LinearLayout
被 RelativeLayout
包装,那么它的 LayoutpParams
是 RelativeLayout.LayoutParams
类型( child 从 parent 获取 LayoutParams
)。您很可能还为代码中使用的 LayoutParams
导入了错误的类。
LinearLayout lay = (LinearLayout) findViewById(R.id.currentTimeMarkerLinearLayout);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)lay.getLayoutParams();
关于java - 类型不匹配 : cannot convert from ViewGroup. LayoutParams 到 LinearLayout.LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13744056/