我有一个小项目,我想在几次重用某个UI组件,所以我通过扩展ViewGroup创建了一个小部件.在该ViewGroup中,我在一个包含LinearLayout内的TextView的视图中膨胀,并通过addView将该膨胀的视图添加到ViewGroup.
外部LinearLayout完美展开,但当我通过Hierarchy Viewer查看时,内部TextView的getHeight()= 0和getWith()= 0.奇怪的是layout_height和layout_width是我在xml中给出的值.
我这里没有代码,但它看起来像这样:
XML:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="random text.."
android:layout_with="200px"
android:layout_height="50px" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Java的:
class MyWidget extends ViewGroup {
...
//In constructor
myView = View.inflate(context, R.layout.xml, null);
addView(myView);
//In layout
myView.layout(l, t, r, b);
Run Code Online (Sandbox Code Playgroud)
我试图给我的文本视图fill_parent值的大小,但它没有帮助.
android ×1