user3578847
user3578847

Reputation: 447

ListView Alignment

I'm new to Android and having problems with ListView control. Whenever I add a ListView to the View, I face the following problems:

  1. The ListView goes 100% width, 100% height, I want to add button on the top and can't do it because of this. I want to manually specify the width,height.

  2. The ListView has some kind of padding enabled, I want it to be 100% width, but it looks like it's 90%

How can I solve these problems? I'm using Eclipse. Thanks!

ListView:

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:padding="0dp"  >
</ListView>

Row:

  <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/rowTextView"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"  
    android:padding="10dp" android:textSize="12sp" >  
   </TextView>  

Complete Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kb.kl.MainActivity" >

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

</ListView>

</RelativeLayout>

Upvotes: 0

Views: 67

Answers (2)

mjp66
mjp66

Reputation: 4213

Use android:layout_weight to prevent listview from hogging all the available space. Example:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/someButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:padding="0dp" />

</LinearLayout>

As for the padding problem, I'm not sure what you're describing.... is it the 10dp padding in your rowTextView that could be causing it?

Upvotes: 1

Stephen Lin
Stephen Lin

Reputation: 4912

If you want to put a button on the top of listview, you should use some layouts to help you, like linearLayout. Here is instruction. http://developer.android.com/guide/topics/ui/layout/linear.html

As for padding of ListView, I don't think there is such kind of setting. Usually, there is setting like divider/entries/dividerHeight in XML. But they all have nothing to do with padding.

Upvotes: 1

Related Questions