2017-06-01 176 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>