在Android中显示复杂的列表视图

时间:2011-11-26 21:14:29

标签: android listview typeface

我正在开发一个需要像这样的ListView的Android项目: enter image description here

每行有三个部分,字体和对齐方式不同。说实话,我研究了很多想法,但没有成功。非常感谢您的指导和帮助。

问候。


CODE


CategoryActivity.java

ArrayList<HashMap<String,String>> list = 
                new ArrayList<HashMap<String,String>>(); 

public class CategoryActivity extends ListActivity 
{

  public void onCreate(Bundle savedInstanceState)
    {


        super.onCreate(savedInstanceState);


        setContentView(R.layout.category);

        list.clear();
         SimpleAdapter adapter = new SimpleAdapter(
                    this,
                    list,
                    R.layout.custom_row_view,
                    new String[] {"contents"},
                    new int[] {R.id.text1}
                    );
            populateList();
            setListAdapter(adapter);


    }

 private void populateList() 
    {
        HashMap<String,String> temp;
        for(int i=0;i<contents.length;i++)
        {
            temp = new HashMap<String,String>();
            temp.put("contents", contents[i]);
            temp.put("str", str[i]);
            temp.put("counts", counts[i]+"");
            list.add(temp);
        }

    }
.

.
.
}

custom_row_view.xml

<?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:orientation="vertical">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
        android:layout_height="fill_parent"

        android:stretchColumns="0"
         android:background="#00000000"
    >

    <TableRow>


         <TextView
        android:id="@+id/text1"
        android:textColor="#FF0000"   
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="right"
        android:textSize="30px"
         android:paddingRight="10dip" 
        >
    </TextView>


<TextView android:id="@+id/text1"         
    android:textColor="#FF0000"   
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"


    android:layout_gravity="right" />       



    </TableRow>


  </TableLayout>



</LinearLayout>

最后是category.xml

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



 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mylayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


        <ListView 
        android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000fff"
    android:layout_weight="2"
    android:drawSelectorOnTop="false">
    </ListView>

</FrameLayout>


</LinearLayout>

实际上上面的代码并没有完全实现图片显示,但它与我使用的非常接近。欢迎您的建议。

2 个答案:

答案 0 :(得分:0)

您需要创建自定义列表布局和自己的适配器。寻找“ListView教程”

答案 1 :(得分:0)

将字体更改为粗体可以通过以下方式在XML中实现:

android:textStyle="bold"

或通过代码:

.setTypeface(Typeface.BOLD);