我想盡可能動態地添加視圖到LinearLayout(取決於屏幕寬度)。在顯示它之前計算視圖度量
我在LinearLayout顯示在屏幕上之前執行此操作。
我的LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#666"/>
我的觀點在LinearLayout中顯示:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#999">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/no_photo"/>
</FrameLayout>
我想補充意見,佈局:
int allItems = 50;
int currentItem = 0;
while(currentItem < allItems)
{
FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fl, null);
linearLayout.addView(view);
if (linearLayout.getMeasuredWidth() >= this.getWidth())
{
linearLayout.removeView(view);
break;
}
}
但linearLayout.getMeasuredWidth()和本。 getWidth()是0;
我知道,我必須使用View.measure方法來計算視圖大小,然後它才變得可見,但我不知道它在哪裏以及如何使用。
的可能欺騙:http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-尺寸的視圖getheight和getwidth總是r/4406090#4406090 – 2012-04-04 07:19:13
檢查此問題http://stackoverflow.com/questions/9498762/dynamically-adding-views-to-horizontal-linearlayout-goes-在屏幕外 – Nishant 2012-04-04 07:21:42
「android:background =」#666「」 - 我看到你正在使用神奇數字就像我做的那樣 – 2017-02-13 16:11:43