【问题标题】:Android inflated view is emptyAndroid 膨胀视图为空
【发布时间】:2010-09-07 21:40:56
【问题描述】:

我正在尝试在垂直 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 而不是普通视图,则会出现分隔符 do

<?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 选项。没有区别。

一个普通的旧视图有什么特别之处使它不会出现吗?

【问题讨论】:

    标签: android view inflate divider


    【解决方案1】:

    我最终通过将分隔视图包装在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多