我正在尝试将一组 View 替换为一个自定义的复合 View ,该 View 应该具有完全相同的功能。具体来说,我经常重复以下布局:
<LinearLayout style="@style/customLayoutStyle">
<Button style="@style/customButtonStyle" />
<TextView style="@style/customTextViewStyle" />
</LinearLayout>
我的目标是用单个 <Highlighter />
替换此 block .
为此,我在 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>
在我的自定义 View 中我有类似的东西
public class Highlighter extends LinearLayout {
public Highlighter(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context, R.layout.highlighter, this);
}
}
这大部分都有效,但似乎是 <merge>
的一些布局参数标签被忽略。这screenshot说明什么似乎是错误的。底行的 3 个图像正确对齐,使用 3x 我试图替换的 LinearLayout block 。只有左上角的图像使用自定义 View 。我的猜测是 padding 和 layout_weight 的布局参数丢失了。我做错了什么,还是需要解决方法?
最佳答案
关于丢失的参数,您是对的。要解决此问题,您可以将荧光笔的样式定义放在定义荧光笔的布局中。
例如
<yournamespace.Highlighter
style="@style/customLayoutStyle"
/>
关于android - 如何在合并的自定义 android View 上维护布局属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14788211/