繁体   English   中英

如何在android中设置ListView列的固定宽度

[英]How to set fixed width of ListView column in android

我想在ListView显示sqlite数据。 但我不知道如何在ListView列中设置固定宽度。

如何制作固定尺寸的色谱柱?

    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_co## Heading ##ntent" 
    android:layout_gravity="center"

ListView的布局

这是我的XML。 我只想在固定布局中创建名称,电话和位置列。

<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"/>

非常感谢。

对于此布局user3336747,您有两种方法:1。对名称,电话,位置使用不同的颜色,并为其值使用相同的颜色。 2.在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>

我认为这会有所帮助。

您需要创建一个自定义列表,此链接更接近您的要求

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

但是你可以通过简单地为listview的自定义项添加适当的权重来实现它。

像这样,首先创建一个自定义布局

<?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>

然后创建一个扩展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 ; 
    }

}

并在yourlistview中添加此内容

class_List.setAdapter(new ClassCustomList());

你也可以看到这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM