当前位置: 动力学知识库 > 问答 > 编程问答 >

sqlite - How to set fixed width of ListView column in android

问题描述:

I would like to display sqlite data in ListView. But I don't know how to set up the fixed width in ListView column.

How can I make the column with fixed size?

 android:id="@android:id/list"

android:layout_width="match_parent"

android:layout_height="wrap_co## Heading ##ntent"

android:layout_gravity="center"

Here is my XML. I just want to make the name,phone and location column in fixed layouts.

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content" >

<Button

android:id="@+id/name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Name:" />

<TextView

android:id="@+id/phoneNo"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_weight="0.5"

android:gravity="center"

android:text="Phone no:" />

<Button

android:id="@+id/location"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Location:" />

</LinearLayout>

tools:context=".Shopping" >

<ListView

android:id="@android:id/list"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"/>

Thanks a lot.

网友答案:

For this layout user3336747 you have two way: 1. Use Different colour for name,phone,location and same for their value. 2. Inflate Below layout in adapter of listview.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="center"
        android:singleLine="true"
        android:text="" />

    <TextView
        android:id="@+id/phoneNo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:singleLine="true"
        android:text="Phone no:" />

    <TextView
        android:id="@+id/location"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="" />
</LinearLayout>

I think it will help.

网友答案:

You shall need to create a custom list this link is closer to you requirement

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

but you can achieve it by simply adding appropriate weight to custom items of listview.

like this , first create a custom layout

<?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="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/classback" >

    <TextView
        android:id="@+id/headingtv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center|left"
        android:layout_weight=".5" 
        android:layout_marginLeft="30dp"
        android:padding="10dp"
        android:textColor="#555555"/>

    <TextView
        android:id="@+id/datetv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_weight=".5"
         android:layout_margin="3dp"
        android:textColor="#555555" />

</LinearLayout>

then create a class extending baseadapter

class ClassCustomList extends BaseAdapter
{

    @Override
    public int getCount() 
    {
        // TODO Auto-generated method stub
        return onelist.size();

    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub


        LayoutInflater inflater ;

        inflater = (LayoutInflater) Class_List.this.
                getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        ViewHolder holder ; 
        if( arg1 == null )
        {

            holder = new ViewHolder();
              arg1 = inflater.inflate(R.layout.classcustomelement, 
                    null);

            holder.Heading = (TextView)arg1.findViewById(R.id.headingtv);

            holder.Date = (TextView)arg1.findViewById(R.id.datetv);

            arg1.setTag(holder);
        }

        else 
        {  

            holder = (ViewHolder)arg1.getTag();

        }

        holder.Heading.setText(twolist.get(arg0));
        holder.Date.setText(fourlist.get(arg0));



        return arg1;
    }


    class ViewHolder 
    {
        TextView Heading , Date ; 
    }

}

and the add this in yourlistview

class_List.setAdapter(new ClassCustomList());

you can see this also

http://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text

分享给朋友:
您可能感兴趣的文章:
随机阅读: