複雜頁面常需「頂部搜尋、中間列表、底部固定操作列」三段式佈局。單純 RelativeLayout + alignParentBottom 易導致中間列表擠壓底部或無法滾動。本文提供穩健的三層佈局解決方案。
RelativeLayout;android:layout_alignParentBottom="true" 固定底部;android:layout_above="@id/bottom" android:layout_below="@id/top" 夾在頂部與底部之間,自適應高度並啟用滾動;<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 頂部搜尋列 -->
<LinearLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/view_user_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="輸入用戶ID"
android:inputType="number"
android:imeOptions="actionDone"
android:layout_margin="6dp"/>
<Button
android:id="@+id/view_user"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="查看"
android:layout_marginTop="4dp"/>
</LinearLayout>
<!-- 中間列表區 -->
<LinearLayout
android:id="@+id/center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="@id/bottom"
android:layout_below="@id/top">
<TextView
android:id="@+id/my_friends_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="好友列表"
android:padding="6dp"
android:layout_marginLeft="10dp"/>
<ListView
android:id="@+id/friends"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="6dp"/>
</LinearLayout>
<!-- 底部固定操作列 -->
<LinearLayout
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="@drawable/bg">
<Button
android:id="@+id/refresh"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="刷新用戶列表"
android:layout_marginTop="2dp"/>
<Button
android:id="@+id/back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="返回"
android:layout_marginTop="2dp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingRight="12dp">
<ImageView
android:id="@+id/friend_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="6dp"/>
<TextView
android:id="@+id/friend_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/friend_image"
android:textSize="18sp"
android:textColor="#ccc"
android:padding="6dp"/>
<TextView
android:id="@+id/friend_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/friend_username"
android:layout_toRightOf="@id/friend_image"
android:paddingRight="2dp"/>
</RelativeLayout>
public class BottomTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = findViewById(R.id.friends);
List<Map<String, Object>> data = new ArrayList<>();
String[] names = {"William","Charles","Linng","Json","Bob","Carli"};
String[] ids = {"12","16","33","21","34","22"};
for (int i = 0; i < 6; i++) {
Map<String, Object> m = new HashMap<>();
m.put("friend_image", R.drawable.icon);
m.put("friend_username", names[i]);
m.put("friend_id", ids[i]);
data.add(m);
}
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[]{"friend_image","friend_username","friend_id"},
new int[]{R.id.friend_image,R.id.friend_username,R.id.friend_id});
list.setAdapter(adapter);
}
}
輸入法彈出時,底部按鈕隨視窗上移,列表區自動縮減並可滾動,不會遮蔽操作列,體驗流暢。