【问题标题】:Set height for a relative layout containing a listView为包含 listView 的相对布局设置高度
【发布时间】:2012-01-11 08:29:23
【问题描述】:

我目前正在开发一个 Android 应用,但我遇到了有关布局的问题。在我的 XML 布局文件中,我创建了一个相对布局(宽度为 fill_parent,高度为 wrap_content),其中包含一个列表视图(或者可能是一个消耗性列表视图 - 高度和宽度的 fill_parent)。在对应的activity onCreate方法中,我只是设置了我的list对应的adapter,并附加到我的list中。问题是,当我运行我的应用程序时,相对布局只有 93dip 的大小,因此当我希望相对布局高度坚持列表当前大小(大小永远不会在创建或恢复活动时通过活动中的操作而改变)。有没有人知道解决这个问题的方法?

一些代码要求:

<LinearLayout android:id="@+id/content" 
              android:layout_width="fill_parent" 
              android:background="@drawable/row" 
              android:layout_below="@+id/header" 
              android:layout_height="wrap_content">
      <ListView android:id="@+id/list" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:background="@drawable/listrow"/>
</LinearLayout>

在 java 文件中(其中 snapRecords 是我的对象列表):

m_panel = (RelativeLayout)findViewById(R.id.lastFareRecordPanel);
ListView lv = (ListView)m_panel.findViewById(R.id.list);
final QuickSnapBookmarksAdapter adapter = new QuickSnapBookmarksAdapter(snapRecords, getApplicationContext(), this, lv);
lv.setAdapter(adapter);

非常感谢您的帮助!

本杰

【问题讨论】:

  • 你能添加布局和一些代码吗?
  • 为什么不用LinearLayout?在你的项目中使用RelativeLayout有什么特别的原因吗?
  • 好吧,理论上我可以修改它,但我不希望这样做,因为这意味着对我和其他从事该项目的人来说会有很多变化......如果我真的必须我会,但我首先想知道我的相对布局问题是否存在解决方案......

标签: android listview layout android-relativelayout


【解决方案1】:

我有类似的情况。当我将父布局更改为LinearLayout时,它正在工作。这是代码:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/prev"
            android:layout_width="106dip"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@null"
            android:onClick="prev"
            android:paddingTop="8dip"
            android:src="@drawable/previous" >
        </ImageButton>

        <ImageButton
            android:id="@+id/next"
            android:layout_width="106dip"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@null"
            android:onClick="next"
            android:paddingTop="8dip"
            android:src="@drawable/next" >
        </ImageButton>

        <ImageButton
            android:id="@+id/Logout"
            android:layout_width="106dip"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@null"
            android:onClick="logout"
            android:paddingTop="8dip"
            android:src="@drawable/logout1"
            android:text="@string/Logout" >
        </ImageButton>
    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="330dip" >
    </ListView>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="338dip"
        android:text="@string/EmptyList"
        android:textSize="35dip" />

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/prev1"
            android:layout_width="106dip"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@null"
            android:onClick="prev"
            android:src="@drawable/previous" >
        </ImageButton>

        <ImageButton
            android:id="@+id/next1"
            android:layout_width="106dip"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@null"
            android:onClick="next"
            android:src="@drawable/next" >
        </ImageButton>

        <ImageButton
            android:id="@+id/Logout"
            android:layout_width="106dip"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@null"
            android:onClick="logout"
            android:src="@drawable/logout1"
            android:text="@string/Logout" >
        </ImageButton>
    </LinearLayout>

</LinearLayout>

【讨论】:

  • 抱歉,我正在测试您的解决方案,同时将一些代码添加到我的问题中......但不是......这在我的情况下不起作用......完全相同的问题......
  • 除了列表视图还有其他视图吗?尝试删除父布局,如果listview是唯一的孩子...
  • 如果这个答案没有解决问题,为什么会被接受?你还需要这个答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
相关资源
最近更新 更多