我正在尝试使用应该完全相同的自定义复合视图替换一组视图。具体来说,我经常重复以下布局:
<LinearLayout style="@style/customLayoutStyle">
<Button style="@style/customButtonStyle" />
<TextView style="@style/customTextViewStyle" />
</LinearLayout>
我的目标是用单个<Highlighter />
替换此块。
为此,我在res / layout / highlighter.xml中定义了类似的内容>>
<merge xmlns:android="http://schemas.android.com/apk/res/android" style="@style/customLayoutStyle"> <Button android:id="@+id/btnHighlighter" style="@style/customButtonStyle" /> <TextView android:id="@+id/lblHighlighter" style="@style/customTextViewStyle" /> </merge>
在我的自定义视图中,我有类似的东西
public class Highlighter extends LinearLayout { public Highlighter(Context context, AttributeSet attrs) { super(context, attrs); inflate(context, R.layout.highlighter, this); } }
这通常有效,但是
<merge>
标记的某些布局参数似乎被忽略了。此screenshot说明了似乎有问题的地方。使用我尝试替换的LinearLayout块的3倍,最下面一行的3张图像已正确对齐。仅左上角的图像使用自定义视图。我的猜测是padding和layout_weight的布局参数丢失了。我是在做错什么,还是需要解决方法?
我正在尝试使用应该完全相同的自定义复合视图替换一组视图。具体来说,我经常重复以下布局:
您对丢失的参数是正确的。要解决此问题,可以将荧光笔的样式定义放在定义荧光笔的布局中。
几年后,这在Android上仍然很复杂,但是可以做到。