【发布时间】:2012-06-12 22:42:03
【问题描述】:
我有一个包含片段的布局。我将 Layout 的宽度设置为 300dip。
我想以编程方式计算子代相对于父代的 300dip 的宽度。
我不能在 XML 中做到这一点,因为它有一些“技巧”并且使用权重或相对布局将不起作用,我必须使用数学。
这是包含布局的片段...
<LinearLayout
android:layout_width="300dip"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<fragment
android:id="@+id/tabbar"
android:name="com.test.MyFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
在 MyFragment 中有方法:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("test", "container: " + container);
View view = inflater.inflate(R.layout.mylayout, container, false);
Log.d("test", "view width: " + view.getWidth());
//do things with view
return view;
}
我得到了输出容器:null 和视图宽度:0,我在片段或类似中找不到任何 onMeasure 方法。
【问题讨论】: