Tôi đang cố tạo hộp thoại tùy chỉnh trong Android. Nhưng bất cứ điều gì tôi đã cố gắng làm, tôi không thể thay đổi chiều rộng của hộp thoại. Nó vẫn giữ nguyên. Sau đây là chế độ xem mà tôi đang đặt làm nội dung cho hộp thoại.Tạo hộp thoại tùy chỉnh trong Android
<RelativeLayout
android:id="@+id/current_stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible">
<ImageView android:id="@+id/player_image"
android:src="@drawable/person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/player_name"
android:layout_below="@id/player_image"
android:layout_centerHorizontal="true"
android:text="Raja Ram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton android:id="@+id/dialog_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="#00000000"
android:src="@drawable/close_button_selector"/>
<ImageButton android:id="@+id/dialog_flip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="#00000000"
android:src="@drawable/rotate"/>
</RelativeLayout>
Như bạn có thể thấy, ở mọi nơi tôi đang sử dụng wrap_content trong mẫu mã này. Ngoài ra tôi đã thử các tùy chọn sau
1) Đặt kiểu tùy chỉnh trong khi tạo Hộp thoại như thế này.
dialog = new Dialog(this,R.style.Theme_Dialog);
Và phong cách như sau
<resources>
<style name="Theme" parent="android:Theme">
</style>
<style name="Theme.Dialog">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
</resources>
2) Thiết lập các tham số cho quan điểm trong onCreateDialog() như thế này
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.player_info, null, false);
ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.setContentView(v,p);
3) Tôi cũng đã cố gắng để thiết lập các cửa sổ các tham số như thế này trong phương thức onCreateDialog()
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width=WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(params);
Nhưng một lần nữa không may mắn. Ai đó có thể giúp tôi với vấn đề này. Tôi có làm điều gì sai?? Ngoài ra, bạn có thể đề nghị tôi làm thế nào để thiết lập các x và y postions cho cửa sổ Dialog?
Cảm ơn
Cảm ơn bạn đã trả lời ur. Vấn đề của tôi là tôi muốn nó giống như một cửa sổ bật lên. Thay vì sử dụng hết toàn bộ chiều rộng của màn hình, tôi chỉ muốn nó chỉ tiêu thụ chiều rộng cần thiết để giữ chế độ xem. Và tôi không bắt đầu một hoạt động riêng biệt trong hộp thoại đó. Tôi quan tâm đến việc hiển thị cửa sổ cho phép người dùng thực hiện một số hành động và nên cho phép vài hoạt ảnh.Tôi thậm chí đã thử PopupWindow nhưng vì một số lý do, hoạt hình tùy chỉnh của tôi không hoạt động với cửa sổ bật lên. Hy vọng tôi đã thực hiện yêu cầu của tôi rõ ràng. Bạn có đề nghị nào không? – rajaramyadhav
Hoạt động theo chủ đề của hộp thoại sẽ trông giống như cửa sổ bật lên. Câu hỏi này cho thấy một ví dụ về nó sẽ trông như thế nào: http://stackoverflow.com/questions/1967085/android-how-to-resize-dialog. –