【问题标题】:i just want to clear this code out我只想清除此代码
【发布时间】:2013-04-07 02:45:24
【问题描述】:

我尝试编写此代码,但我在页眉和页脚应该加载的部分出现错误..我从这篇帖子Android ListActivity - fixed header and footer得到这个我只是检查希望这不会导致人们出错代码...错误说 错误:在包 'android' 中找不到属性 'above' 的资源标识符 错误:在包 'android' 中找不到属性 'below' 的资源标识符

或者我完全错了,当我错了时忽略它

 In main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <include
    android:id="@+id/header_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    layout="@layout/header.xml" />

 <ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:below="@id/header_layout"
    android:above="@id/footer_layout" />

   <include
    android:id="@+id/footer_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    layout="@layout/footer.xml" />
  </RelativeLayout>
In header.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView
       android:id="@+id/header_text_view"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:text="Your Application Title"/>
 </LinearLayout>
In footer.xml`enter code here`

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

  <Button
       android:id="@+id/done_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:text="Done"/>
 </LinearLayout>

在活动中,这里

  public class MainActivity extends ListActivity {

 private ListView mListView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mListView = (ListView) findViewById(R.id.list_view);
    // Now you can do whatever you want

   }
 }

【问题讨论】:

    标签: android


    【解决方案1】:

    在 main.xml 中将 android:below 更改为 android:layout_below 并将 android:above 更改为 android:layout_above

    编辑: 还有其他方法可以实现这一点,可能会更好。

    选项 A:改为使用垂直 LinearLayout。注意列表视图的布局属性;这些将允许它拉伸以填充页眉和页脚视图未占用的空间。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <include
            android:id="@+id/header_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/header.xml" />
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <include
            android:id="@+id/footer_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            layout="@layout/footer.xml" />
    </LinearLayout>
    

    选项 B:如果你不调用 setContentView(),ListActivity 会自动为你提供一个 ListView。在这种情况下,您可以自己创建或扩充页眉和页脚视图,并将它们添加到代码中的列表视图中。

    public void onCreate(Bundle saved) {
        super(saved);
        ListView list = getListView();
        LayoutInflater inflater = getLayoutInflater();
        View headerView = inflater.inflate(R.layout.header, list, false);
        View footerView = inflater.inflate(R.layout.footer, list, false);
    
        // these must be called before list.setAdapter()
        list.addHeaderView(headerView);
        list.addFooterView(footerView);
        /* ... */
    }
    

    【讨论】:

    • 另外,由于您使用的是 ListActvity,因此您的 ListView 元素应该有android:id="@android:id/list"。在您的活动中,您将使用mListView = getListView();
    • 为什么header还是有错误id是同一个header却一直说no resource found so wierd....
    • 说真的吗?这是页眉和页脚的代码吗?为什么我的结果显示在中心大声笑愚蠢我无法正确处理..
    • 就个人而言,我会采取不同的做法。 选项 A:使用垂直 LinearLayout 而不是 RelativeLayout。保持视图的顺序,但使 ListView 有layout_height="0dp"layout_weight="1"选项 B:根本不使用布局。如果你不调用setContentView(),ListActivity 已经自动为你提供了一个 ListView。在设置列表适配器之前,您必须以编程方式添加页眉和页脚视图。
    • 你能把它说得更清楚一点吗?比如在代码中表现出糟糕的英语和糟糕的编程 android 技能上个月才开始编程..还在学习
    猜你喜欢
    • 2017-10-25
    • 2013-06-04
    • 2022-12-19
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多