beg*_*ass 6 android dialog width
我想设置自定义对话框的宽度以包装内容,但始终将其填满屏幕的所有宽度
我已经测试过了
android.view.WindowManager.LayoutParams params = mydialog.getWindow().getAttributes();
params.width = android.view.WindowManager.LayoutParams.WRAP_CONTENT;
mydialog.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
Run Code Online (Sandbox Code Playgroud)
然后
mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Run Code Online (Sandbox Code Playgroud)
我也很难解决这个问题,最终找到了更好的解决方法。
这是解决方法。在你的代码中
mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Run Code Online (Sandbox Code Playgroud)
这是行不通的,因为ProgressDialog的默认布局具有match_parent和左右边距。要调整ProgressDialog的大小,您需要如下所示的“自定义视图”:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:drawable/dialog_holo_light_frame"
android:layout_gravity="center_horizontal"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/progressBar"/>
<TextView
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:gravity="center"
android:id="@+id/message"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
并在显示ProgressDialog之后对其进行充气。放大自定义视图后,使用GlobalLayoutListener获得自定义视图的宽度并设置布局宽度。
mDialog.show();
final Window window = mDialog.getWindow();
window.setContentView(R.layout.custom_progress_dialog); // this is the above code
final LinearLayout dialogContainer = (LinearLayout) window.findViewById(R.id.container);
TextView message = (TextView) window.findViewById(R.id.message);
message.setText("Loading . . .");
dialogContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int width = dialogContainer.getWidth();
window.setLayout(width, WindowManager.LayoutParams.WRAP_CONTENT);
dialogContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Run Code Online (Sandbox Code Playgroud)
我知道这个问题很旧,但是如果有人不想添加 WindowManager.LayoutParams,那么只需将对话框的主题设置为此android.R.style.Theme_Holo_Light_Dialog_NoActionBar将使对话框将其宽度包裹到其内容中。
例子:
AlertDialog alert = new AlertDialog.Builder(context, android.R.style.Theme_Holo_Light_Dialog_NoActionBar).create();
确保您要膨胀的布局的wrap_content高度和宽度均已设置。
vip*_*tal -1
使用以下属性将样式设置为自定义对话框:
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:windowIsFloating">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10810 次 |
| 最近记录: |