【问题标题】:Android dialog wrap_content not workingAndroid对话框wrap_content不起作用
【发布时间】:2016-07-27 23:55:13
【问题描述】:

我设置了一个自定义对话框,并将布局的宽度设置为wrap_content,但它仍然占据整个屏幕宽度。我什至尝试将宽度设置为 100dp 并且它仍然使用整个屏幕为什么会发生这种情况?

我已经设置了这样的对话框:

public class DialogGoToLine extends Dialog
{
//The root view of the dialog
private static View rootView;

public DialogGoToLine(Context context, int themeResId){
    super(context, themeResId);
}

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //Allow the dialog to be canceled by tapping out of its bounds
    setCancelable(true);

    //Inflate the custom layout
    LayoutInflater layoutInflater = LayoutInflater.from(HotspotHelper.getAppContext());
    rootView = layoutInflater.inflate(R.layout.dialog_go_to_line, null);

    //Set up the layout
    //setTitle(HotspotHelper.getAppContext().getString(R.string.clientDetails));
    //setTitle("Go to line");
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(rootView);

    final EditText editText = (EditText) rootView.findViewById(R.id.editTextGoToLine);

    Button buttonGoToLine = (Button) rootView.findViewById(R.id.buttonGoToLine);
    buttonGoToLine.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View p1)
            {
                int line = Integer.valueOf(editText.getText().toString()) - 1;
                ((LinearLayoutManager)LogActivity.recyclerViewLog.getLayoutManager()).scrollToPositionWithOffset(line, (int)HotspotHelper.dpToPx(30));
                dismiss();
            }
    });
}
}

还有布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
    android:id="@+id/textViewGoToLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:labelFor="@+id/editTextGoToLine"
    android:text="Go to line: "
    android:background="@color/accent"
    android:textColor="@color/primaryText"
    android:textSize="18sp"/>

<EditText
    android:id="@id/editTextGoToLine"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:layout_margin="4dp"
    android:layout_centerVertical="true"
    android:inputType="number"
    android:gravity="center"
    android:textSize="18sp"/>

<Button
    android:id="@+id/buttonGoToLine"
    android:layout_width="wrap_content"
    android:text="Go"
    android:layout_margin="4dp"
    android:textSize="18sp"
    android:layout_height="wrap_content"/>

编辑 1 如果有什么不同,这个对话框有一个样式,我在其中声明了这个drawable的背景:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/colorPrimary"/> <corners android:radius="2dp" /> </shape>

【问题讨论】:

  • 远景:我在 Studio 的 Instant Run 中遇到了与对话框相关的错误。如果您正在使用,是否可以尝试卸载该应用并重试?
  • 你的父linear_layout宽度是100dp
  • @Shaishav 我目前在我的手机上使用 AIDE,它没有即时运行功能
  • @vinoth12594 我试过 100dp 但问题是它仍然占据整个屏幕

标签: java android


【解决方案1】:

使用必须使用此方法setContentView(View view, ViewGroup.LayoutParams params) 而不是setContentView(rootView);

ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
//ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
//        300,
//       300);
//for coustom size
params.height = thumb_size;
params.width = thumb_size;
// set padding ,margins ,gravity as your desired

使用这个setContentView(rootView, params);

【讨论】:

  • getLayoutParams() 给了我一个 NPE
  • 创建具有所需宽度和高度的新布局参数,这是布局参数的示例代码。
  • 我已经解决了这个问题,方法是从自定义主题中删除背景属性并将其应用到父布局而不是对话框本身
【解决方案2】:

我遇到了同样的问题,我遇到了一个解决方案。如果您为布局的根提供固定宽度或 wrap_content,则不会考虑这一点。我不知道这背后的原因。在您的根目录中再放置一个父布局,并为内部父级提供所需的宽度和高度,将所有视图放在该父级中。希望对你有帮助。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        //put all your views inside this linear layout

        <TextView......./>
        <TextView......./>
        <TextView......./>
    </LinearLayout>

</LinearLayout>

【讨论】:

  • 可惜没用,对话框屏幕右侧还有一堆空白
【解决方案3】:

在显示对话框之前试试这个:

dialog.getWindow().setLayout(context.getResources().getDimensionPixelSize(R.dimen.dialog_width), WindowManager.LayoutParams.WRAP_CONTENT)

dialog_width100dp

【讨论】:

    【解决方案4】:

    出于某种原因,删除我应用于对话框的主题修复了 wrap_content 问题。自定义主题所做的唯一一件事就是设置对话框的背景,但删除它已经解决了问题。

    【讨论】:

      【解决方案5】:

      尝试更改主线性布局中的android:orientation="vertical",因为您使用的是水平方向的线性布局,因此所有小部件都水平对齐,并且它们正在水平获取分配的空间,这就是它覆盖整个屏幕的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多