效果
最後是實現了RecyclerView
下面的控件會跟隨着內容增加而下滑,並且最後超過屏幕時,下面的控件會粘在屏幕底部
代碼
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something here"
android:id="@+id/input_text"
android:maxLines="2"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/send"
android:text="send"
/>
</LinearLayout>
</LinearLayout>
注意點
其實爲什麼會是下面的組件跟隨RecyclerView
進行變動的呢?
因爲我們可以看到最外層LinearLayout
的高度android:layout_height
是wrap_content
,所以,它其實是在適應整個RecyclerView
的高度進行變化。
推論
如果把最外層LinearLayout
的高度android:layout_height
改爲match_parent
,那麼,RecyclerView
下面的控件將會一直保持在最底層
代碼
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/msg_recycler_view"
></android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something here"
android:id="@+id/input_text"
android:maxLines="2"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/send"
android:text="send"
/>
</LinearLayout>
</LinearLayout>