2017-06-01 172 views
-2

我想定制自己的Android中的ListView像自定义列表视图中的Android

this

什么,我究竟想要实现我想在每个ListView项和左侧添加这些颜色当然,我想为每个项目使用不同的颜色。我怎样才能做到这一点?

这是我的列表视图是代码:

<RelativeLayout 
    android:id="@+id/hidden_panel" 
    android:layout_width="match_parent" 
    android:layout_marginTop="56dp" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:visibility="gone" > 

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

    <Button 
     android:id="@+id/button7" 
     android:layout_width="300dp" 
     android:layout_height="wrap_content" 
     android:textColor="#fff" 
     android:textSize="18sp" 
     android:layout_marginTop="5dp" 
     android:text="Compute admission chance" 
     android:textAllCaps="false" 
     android:background="@drawable/roundshapebtn" 
     android:layout_below="@id/listView1" 
     android:layout_centerInParent="true" /> 
</RelativeLayout> 

编辑: 这是我为我的列表视图项:

ListView lv = (ListView) findViewById(R.id.listView1); 
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_layout,listRecNames); 
       lv.setAdapter(adapter); 

布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="95dp" 
    android:divider="?android:dividerVertical" 
    android:showDividers="middle" > 
    <!-- You can use any view here. Set background "rectangle.xml" drawable to this view.--> 
    <View 
     android:layout_width="20dip" 
     android:layout_height="20dip" 
     android:background="@drawable/rectangle" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     /> 

    <TextView 
     android:id="@+id/custom_spinner" 
     android:textSize="16sp" 
     android:padding="10dp" 
     android:textColor="#000" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

但我得到这个错误:

ArrayAdapter requires the resource ID to be a TextView

+0

到目前为止您尝试了什么?你失败了? – Selvin

+0

将该视图添加到您的listview项目的xml文件中。 –

+0

您需要使用CollapsingToolbarLayout和RecyclerView的组合。 –

回答

0

你需要一个实现getView()膨胀你的View,做你想要什么用的ArrayAdapter自定义实现。

您使用的是一个通用适配器,它适用于只有一个文本要在TextView s的列表中显示的列表项。

欲了解更多信息,可参考的教程,如:Custom ArrayAdapters

0

为了使设计像第一个图像,你可以使用下面的XML布局设计。使用自定义视图设计将您的列表替换为card_layouts。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="250dp" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 


      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="centerCrop" 
       android:fitsSystemWindows="true" 
       android:background="@drawable/profile_pic" 
       android:id="@+id/profile_id" 
       app:layout_collapseMode="parallax" /> 


      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" /> 

     </android.support.design.widget.CollapsingToolbarLayout> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/abc_search_url_text_normal"/> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#ffe5e5e5" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

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

      <include layout="@layout/card_layout" /> 

      <include layout="@layout/card_layout" /> 

      <include layout="@layout/card_layout" /> 

     </LinearLayout> 


    </android.support.v4.widget.NestedScrollView> 

</android.support.design.widget.CoordinatorLayout>