2016-10-21 42 views
-2

我想用自定義項目創建一個列表。因此,我創建了一個新list_item.xml像本教程: http://www.tutorialsbuzz.com/2014/03/watsapp-custom-listview-imageview-textview-baseadapter.htmlAndroid - 標題欄和動作欄消失後,自定義列表項目的xml

在我activity_main.xml中它是這個樣子Android Studio中:

screenshot1

但在模擬器它顯示如沒有titleBar和actionBar也是如此:

screenshot2

list_item.xml

<?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="match_parent" 
    android:padding="2dp"> 
    <ImageView 
     android:id="@+id/profile_pic" 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:contentDescription="desc" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" /> 

    <TextView 
     android:id="@+id/alias" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:layout_toRightOf="@+id/profile_pic" 
     android:paddingBottom="10dp" 
     android:text="txt" 
     android:textSize="20sp" /> 

    <TextView 
     android:id="@+id/public_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="txt" 
     android:textSize="16sp" 
     android:layout_below="@+id/alias" 
     android:layout_toEndOf="@+id/profile_pic" /> 

    <TextView 
     android:id="@+id/contact_type" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="txt" 
     android:textSize="16sp" 
     android:layout_alignBottom="@+id/alias" 
     android:layout_alignParentEnd="true" /> 
</RelativeLayout> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.Toolbar 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:theme="?attr/actionBarTheme" 
      android:minHeight="?attr/actionBarSize" 
      android:id="@+id/main_toolbar" /> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:srcCompat="@drawable/ic_locked" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginStart="13dp" 
      android:id="@+id/btn_lock" /> 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/main_toolbar" 
      android:layout_alignParentStart="true" 
      android:id="@+id/contact_list" /> 

    </RelativeLayout> 

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

的其他代碼,如自定義適配器是像一些重命名變量的差別教程/ IDS。另外,我的MainActivity.java擴展了除Activity之外的AppCompatActivity。

回答

0

嘗試此佈局,對於協調佈局內要顯示的工具欄, 它必須是內部AppBarLayout(這又必須是CL的第一個孩子)

剩餘內容的視圖可以是具有ScrollFlags

任何佈局
<android.support.design.widget.CoordinatorLayout 
     <AppbarLayout> 
       <toolbar> 
     </AppBarLayout> 
    //Main Content View 
    <RelativeLayout 
     app:scroll_flags = "@string/scrollingBehaviour"-->this is important` 
</RelativeLayout>