没有足够的空间用于ListView

时间:2013-08-13 01:28:30

标签: android android-layout

我在LinearLayout上有TextView和ListView,不幸的是在小屏幕上我没有足够的空间用于ListView(我的意思是它显示大约0.5row)。是否可以将所有内容向上滚动。?在SDK中我可以使用它的任何替代ListView。或者也许您还有其他想法如何使用户友好。?

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/studio"
        android:textAppearance="?android:attr/textAppearanceMedium" />

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

</LinearLayout>

    

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2" >
</ListView>
</LinearLayout>

4 个答案:

答案 0 :(得分:0)

您的问题是您分配给各种组件的尺寸和重量。为listview和文本视图布局分配适当的权重,它应该可以在任何屏幕上正常工作。例如做一些这样的事情:

    <LinearLayout 
          android:id= "@+id/parent_layout"
          android:layout_height = "match_parent"
          android:layout_width = "match_parent"
          android:weightSum = "1.0"
          android:orientation = "vertical" >

          <LinearLayout
             android:id= "@+id/text_views_layout"
             android:layout_height = "0dp"
             android:layout_width = "match_parent"
             android:layout_weight = "0.7" 
          >
              <!-- place your text views here -->

          </LinearLayout>

          <ListView
             android:id ="@+id/list"
             android:layout_height = "0dp"
             android:layout_width ="match_parent"
             android:layout_weight = "0.3" 
          >
          </ListView> 

   </LinearLayout> 

注意:您可以为textview_layout和listview指定任何权重比例(我已将其设置为0.7:0.3)但记住您为其分配的权重,它们必须总计为父项的权重总和,即在这种情况下为1.0。否则您可能会看到意外行为。

答案 1 :(得分:0)

使用addHeaderViewLinearLayoutTextView作为ListView的标题视图放置

答案 2 :(得分:0)

There is a better solution for this instead of adding title to the main layout you can add that title to another layout and inflate that layout as header to the listview.

Like this -:

LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.title, myListView, false);
myListView.addHeaderView(header, null, false);

layout.xml

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

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

</LinearLayout>


title.xml
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/studio"
        android:textAppearance="?android:attr/textAppearanceMedium" />

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

</LinearLayout>

答案 3 :(得分:-1)

ScrollView可以解决您的问题,例如

<ScrollView>
   <LinearLayout>
    ......
   //More code here

   </LinearLayout>

</ScrollView>

但是请记住Scroll View只能只有一个孩子,而且那个孩子可以拥有自己的另一个孩子,但ScrollView只能有一个孩子。我的这段代码是为了让你知道它只是复制和粘贴你不能工作你必须根据你的需要调整宽度,高度等