0
我試圖創建一個對話框片段需要寬度的98%和高度的Android對話片段變化大小
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
int width = (int)(0.98 * getResources().getDisplayMetrics().widthPixels);
int height = (int)(0.9 * getResources().getDisplayMetrics().heightPixels);
dialog.getWindow().setLayout(width, height);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
View root = getActivity().getLayoutInflater().inflate(R.layout.dialog_collector_rating, null);
getReference(root);
dialog.setContentView(root);
return dialog;
}
private void getReference(View root){
if(mRates == null){
mRates = new ArrayList<TopRateDataItem>();
}
// list
ListView ratingList = (ListView) root.findViewById(R.id.ratingList);
RatingAdapter adapter = new RatingAdapter();
ratingList.setAdapter(adapter);
// workers number
TextView tvWorkersNum = (TextView) root.findViewById(R.id.tvWorkersNum);
tvWorkersNum.setText(getString(R.string.dialog_collector_rating_workers, mRates.size()));
// button
View btnClose = root.findViewById(R.id.btnClose);
btnClose.setOnClickListener(this);
}
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//
// if(mRates == null){
// mRates = new ArrayList<TopRateDataItem>();
// }
//
// // Inflate view
// View root = getActivity().getLayoutInflater().inflate(R.layout.dialog_collector_rating, null);
//
// // list
// ListView ratingList = (ListView) root.findViewById(R.id.ratingList);
// RatingAdapter adapter = new RatingAdapter();
// ratingList.setAdapter(adapter);
//
// // workers number
// TextView tvWorkersNum = (TextView) root.findViewById(R.id.tvWorkersNum);
// tvWorkersNum.setText(getString(R.string.dialog_collector_rating_workers, mRates.size()));
//
// // button
// View btnClose = root.findViewById(R.id.btnClose);
// btnClose.setOnClickListener(this);
//
// return root;
// }
佈局的90%百分比:
<?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="@android:color/white"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:background="@color/dialog_title_background"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/dialog_title_text_color"
android:text="@string/dialog_collector_rating_title"/>
<ListView
android:id="@+id/ratingList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="25dp"
android:paddingLeft="25dp"
android:background="@drawable/button_grey_selector"
android:textStyle="bold"
android:textColor="@android:color/white"
android:text="@string/dialog_collector_rating_button_text"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right|center_vertical">
<TextView
android:id="@+id/tvWorkersNum"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textStyle="bold"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/dialog_collector_rating_all"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
我onCreateDialog
試過和'onCreateView'在這兩種情況下,即使試圖輸入硬編碼的寬度和高度,對話框也不會發生變化。
如何使該權利?
題外話:你想用'dialog.getLayoutInflater(...)'膨脹對話框的內部。想象一下你有黑暗的活動和白色的對話。 –
我試過了,不行 – NickF