在Android中创建自定义对话框

raj*_*hav 14 android dialog

我正在尝试在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>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我在这个代码示例中使用wrap_content.我也尝试了以下选项

1)在创建对话框时设置自定义样式.

dialog = new Dialog(this,R.style.Theme_Dialog);
Run Code Online (Sandbox Code Playgroud)

风格如下

<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>
Run Code Online (Sandbox Code Playgroud)

2)像这样在onCreateDialog()中设置视图的参数

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);
Run Code Online (Sandbox Code Playgroud)

3)我还尝试在onCreateDialog()方法中设置这样的Window参数

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width=WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(params);
Run Code Online (Sandbox Code Playgroud)

但再次没有运气.有人可以帮我解决这个问题.难道我做错了什么??你也可以建议我如何设置对话窗口的x和y位置?

谢谢

Jit*_*hin 31

        dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);

        dialog.setContentView(R.layout.custom_dialog);

         LayoutParams lp=dialog.getWindow().getAttributes();    
         lp.x=100;lp.y=100;lp.width=100;lp.height=200;lp.gravity=Gravity.TOP | Gravity.LEFT;
         lp.dimAmount=0;            
         lp.flags=LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_NOT_TOUCH_MODAL;
    //   dialog.getWindow().setAttributes(lp);

         dialog.show();
Run Code Online (Sandbox Code Playgroud)

这对我很有用....


raj*_*hav 1

我花了很长时间才发现问题。问题出在我使用相对布局的方式上。我想我没有正确指定相对位置。当我将其更改为线性布局时,效果很好。它并没有占用整个屏幕,而是仅占用所需的内容。