即使所有父级布局的layout_width都设置为fill_parent,对话框仍在缩小。

3
在这里遇到了另一个奇怪的行为。尽管将所有父布局的宽度设置为fill_parent,但我的对话框在宽度方面仍然缩小。以下是图片...enter image description here 我尝试将其作为具有对话框主题的活动进行设置。但它的行为仍然相同。但是,只要我将第一个TextView“用户协议”的layout_width设置为fill_parent,它就变得正常了。但我不理解这种行为,因为它不应该依赖于TextView的宽度来确定自己的宽度。请告诉我是否有其他有效的方法来处理这些类型的情况。我的布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@layout/gradientback"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="User Agreement"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#B80303" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="3dip"
        android:background="?android:attr/listDivider" >
    </View>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/checkBoxForTermsId"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#7B4302"
            android:text="I Agree The Terms &amp; Conditions" >
        </CheckBox>

这不是整个布局的代码,因为我认为它不是必需的...

显示对话框的代码如下:

private void showAgreementBox() {
        // TODO Auto-generated method stub
        final Dialog dialog = new Dialog(Launcher.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.useragreement);
        dialog.setTitle("User Agreement");
        dialog.setCancelable(false);
        final TextView userAg = (TextView) dialog.findViewById(R.id.textViewOfUserAg);
        final CheckBox checkUserAg = (CheckBox) dialog.findViewById(R.id.checkBoxForTermsId);
        final Button continueB = (Button) dialog.findViewById(R.id.continueB);
        checkUserAg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (checkUserAg.isChecked() == true) {
                    continueB.setEnabled(true);
                } else {
                    continueB.setEnabled(false);
                }
            }
        });

        continueB.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.dismiss();
                //checkForTrialPeriod(isUserRegisterd);
            }
        });

        Button cancelB = (Button) dialog.findViewById(R.id.cancelB);
        cancelB.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        dialog.show();
    }

该单词是“example”,而不是“exemple”! :-)例子 - bofredo
1
@bofredo:如果我说它是 Temple,怎么样... :) - Vikram Singh
呵呵呵,我也曾考虑过这个问题,但我认为胜算不大 ;) - bofredo
嗯...看看是否有其他人遇到过这个奇怪的问题... :) - Vikram Singh
@VikramSingh,请发布您创建对话框的代码。 - JJ86
显示剩余2条评论
2个回答

4
setContentView(R.layout.layout_name);
    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

这对我有效。

4

调整父容器的宽度

以解决此问题。
 android:layout_width="400dp"

在Activity的xml中,fill_parent可以使用,但对于对话框,您需要设置宽度。

如果您正在寻找通用解决方案,请在父布局中设置一个图像作为背景,就像我们将图像放置在不同的drawable文件夹中以支持多个屏幕一样。

您还可以使用全屏宽度进行设置:

dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;

从Dianne Hackborn的帖子中引用,对话框显示此行为的原因是:

对话框主题会将顶层窗口布局设置为WRAP_CONTENT。你可以尝试手动将窗口布局宽度和高度设置为FILL_PARENT [...]

这个答案也提供了参考。

2
我认为他正在寻找一种更通用的解决方案,适用于不同的设备。 - bofredo
我已经尝试将图像放置在背景中,这解决了问题,但我不想这样做,因为它会增加我的应用程序的大小。但问题是为什么它取决于我的标题textview的宽度而不是任何其他布局或视图... - Vikram Singh
再次更新答案。 - Jitender Dev
你的回答的后半部分有效...谢谢...对于对话主题的信息加一(赞)... :) - Vikram Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接