[英]Android inflated view is empty
我試圖在垂直LinearLayout中的條目之間添加一個分隔線,以模擬ListView的外觀。 (在這種特殊情況下,我不能只使用ListView。)
這是我在list_divider.xml中的內容:
<?xml version="1.0" encoding="utf-8"?>
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/panel_border"
android:layout_width="fill_parent"
android:layout_height="@dimen/border_width"
/>
這是代碼,試圖在除列表中第一個元素之外的每個元素之前膨脹此分隔符:
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < categories.size(); i++ ) {
if (i > 0)
items.addView(vi.inflate(R.layout.list_divider, null));
// these dividers never appear
// but these main entries always appear just fine
items.addView(ad.getView(i, null, null));
}
主列表項正確顯示,但分隔符不可見。
如果將分隔符更改為TextView而不是普通View,分隔符的確會出現:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/panel_border"
android:layout_width="fill_parent"
android:layout_height="@dimen/border_width"
android:text="---"
/>
我嘗試設置寬度和高度的顯式像素值,以及使用border_width定義和fill_parent選項。 這沒什么區別。
普通的舊視圖有什么特別之處,使其無法顯示?
我最終通過將分隔器視圖包裝在FrameLayout中來解決此問題。 似乎只有一個沒有內容的空視圖是行不通的。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<View
android:background="@color/panel_border"
android:layout_width="fill_parent"
android:layout_height="@dimen/border_width" />
</FrameLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.