我创建了自己的 ViewGroup,目的是在其区域内均匀分布 4 个元素。
ViewGroup 本身在宽度和高度上都设置为 match_parent,上面有一个 TextView:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/relativeLayout"
android:paddingBottom="16"
android:paddingLeft="16"
android:paddingRight="16"
android:paddingTop="16">
<TextView
android:id="@+id/title_actions"
style="@style/h2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_dashboard"
android:textColor="@color/colorAccent" />
<app.xenon.sync.layout.dashboard
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/title_actions"
android:layout_marginTop="16dp">
<!-- 4 elements here -->
</app.xenon.sync.layout.dashboard>
</RelativeLayout>
手头的问题是如何从自己的类中获取 ViewGroup 本身的宽度,然后分配子项。我会假设这将在 onMeasure 方法中完成,但是在挖掘了这里和 Javadoc 之后,我找不到任何可以帮助我的东西。
所以这就是我找到你的原因!