我的 Android 应用程序中有几个具有相同基本结构的活动,并且我正在尝试使我的布局干燥。重复的代码如下所示。它包含一个带有页脚的可滚动区域,该区域具有“返回”和“仪表板”链接。还有一个 FrameLayout 用于在可滚动区域的顶部应用渐变。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ScrollView
android:layout_width="match_parent"
android:layout_height="689px">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- THE REAL PAGE CONTENT GOES HERE -->
</LinearLayout>
</ScrollView>
<ImageView
android:src="@drawable/GradientBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50px"
android:background="?attr/primaryAccentColor">
<Button
android:layout_width="wrap_content"
android:layout_height="26px"
android:layout_gravity="center_vertical"
local:MvxBind="Click GoBackCommand" />
<Button
android:layout_width="wrap_content"
android:layout_height="26px"
local:MvxBind="Click ShowDashboardHomeCommand" />
</FrameLayout>
</LinearLayout>
要对我的活动进行重复数据删除,我认为我需要做的是创建一个从 LinearLayout 继承的自定义 ViewGroup。在该代码中,从 XML 文件加载上述内容。我迷路的地方是如何让 Activity 中的子内容加载到正确的位置。例如,假设我的 Activity 现在包含:
<com.myapp.ScrollableVerticalLayoutWithDashboard
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- THE REAL PAGE CONTENT GOES HERE -->
<TextView android:text"blah blah blah" />
</com.myapp.ScrollableVerticalLayoutWithDashboard>
现在我如何使“blah blah blah”出现在正确的位置?我很确定如果我这样做了,我最终会在页面顶部或底部出现“blah blah blah”,而不是根据需要在 ScrollView 的中间。
我正在使用 API 21 / v5.0+。从技术上讲,我正在用 Xamarin 做这一切,但希望这与答案无关?
编辑:结果的一个例子是这样的。页脚和渐变是自定义 ViewGroup 的一部分,但其余部分将是自定义 ViewGroup 中的内容。