我试图创建一个自定义的Android控件,其中包含一个LinearLayout。可以将其视为带有花哨边框、背景和左侧图像的扩展LinearLayout。
我可以在XML中完成所有操作(效果很好),但由于我的应用程序中有数十个出现,所以很难维护。我认为最好有类似这样的东西:
/* Main.xml */
<MyFancyLayout>
<TextView /> /* what goes inside my control's linear layout */
</MyfancyLayout>
你会如何解决这个问题?我想避免重写整个线性布局的 onMeasure / onLayout 方法。目前我的代码如下:
/* MyFancyLayout.xml */
<TableLayout>
<ImageView />
<LinearLayout id="container" /> /* where I want the real content to go */
</TableLayout>
并且。/* MyFancyLayout.java */
public class MyFancyLayout extends LinearLayout
{
public MyFancyLayout(Context context) {
super(context);
View.inflate(context, R.layout.my_fancy_layout, this);
}
}
如何将用户指定的内容(main.xml中的TextView)插入到正确的位置(id = container)?
谢谢!
Romain
----- 编辑 -------
仍然没有找到解决方法,所以我改变了我的设计,使用更简单的布局,并决定接受一些重复的XML。不过仍然对任何知道如何实现这个功能感兴趣!