這裏是你的問題的動態解決方案.. 樣品對話框XML文件名activity_list_test.xml
..
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/page_bg"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header Button" />
<ListView
android:id="@+id/lvCommon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom Button" /></LinearLayout>`
下面是一個片段/活動,你要調用..
private void showAlert() {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_list_test, null);
dialog.setView(view);
dialog.setTitle(null);
dialog.setMessage(null);
ArrayList<String> mList = new ArrayList<>();
for (int i = 0; i < 15; i++) {
mList.add("ABC");
}
ListView lvCommon = (ListView) view.findViewById(R.id.lvCommon);
int deviceHeight = getScreenSize(getActivity()).y;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, deviceHeight/3,1f);
lvCommon.setLayoutParams(layoutParams);
lvCommon.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, mList));
dialog.setCancelable(false);
dialog.show();
}
有設備高度將自動測量,並根據它的對話框大小將是"deviceheight/3"
..所以它將在所有設備保持..
現在你需要的方法getScreenSize
..所以這裏是::
public static Point getScreenSize(Activity act) {
int screenWidth = 0;
int screenHeight = 0;
final DisplayMetrics metrics = new DisplayMetrics();
act.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
return new Point(screenWidth, screenHeight);
}
母艦一樣拿不出頁腳出入畫面時,如果列表視圖是大? –
@AbdulSalamAli如果你的數據很大,那麼更好的方法是你必須給「ListView」修正「height」,如果你不喜歡這種方式,沒有其他選擇,那麼你就改變了結構。 – Ironman
是的,我剛剛檢查過,如果列表視圖太大,頁腳跳出屏幕 –