DialogFragment中子视图的getWidth 我有一个名为DF.java的DialogFragment类,它使用一个名为DF.xml的基于linearlayout的布局 DF.xml中的子元素(同样是linearlayouts)的宽度设置为0dp,并使用权重进行计算 我计划在单击按钮时显示活动中的DialogFragment DF,它工作正常,DialogFragment加载正常
现在,当DialogFragment DF出现时,我需要获得它的一个子元素(linearlayout)的宽度 我在onCreateDialog中尝试了getWidth、GetMeasuredWidth、ViewTreeObserver等,但结果始终为零 显示对话框片段的代码DialogFragment中子视图的getWidth 我有一个名为DF.java的DialogFragment类,它使用一个名为DF.xml的基于linearlayout的布局 DF.xml中的子元素(同样是linearlayouts)的宽度设置为0dp,并使用权重进行计算 我计划在单击按钮时显示活动中的DialogFragment DF,它工作正常,DialogFragment加载正常,java,android,android-layout,android-dialogfragment,Java,Android,Android Layout,Android Dialogfragment,现在,当DialogFragment DF出现时,我需要获得它的一个子元素(linearlayout)的宽度 我在onCreateDialog中尝试了getWidth、GetMeasuredWidth、ViewTreeObserver等,但结果始终为零 显示对话框片段的代码 DF dialog = DF.newInstance(context, "DFInstance"); dialog.show(((Activity)context).getFragmentManager(),
DF dialog = DF.newInstance(context, "DFInstance");
dialog.show(((Activity)context).getFragmentManager(), "DFInstance");
DialogFragment DF.java类中的代码
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(context);
View dialog_layout = inflater.inflate(R.layout.dialog_DF,
null);
CurrentDialog = new Dialog(context, R.style.DFTheme);
CurrentDialog.setCanceledOnTouchOutside(true);
CurrentDialog.setContentView(dialog_layout);
CurrentDialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
CurrentDialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
//Tried getting width here using many approaches, Ex:
int Width1 = (((Dialog)dialog).findViewById(R.Id.fieldForWidth)).getWidth();
// Also tried using MeasureSpec here and then getMeasuresWidth
// Also tried adding above code in ViewTreeObserver here.
}
});
Window window = CurrentDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
CurrentDialog.show();
return CurrentDialog;
}
但奇怪的是,如果我选择DialogFragment,并直接从我的主要活动中显示对话框,那么同一代码将使用getWidth()在onShow中完美地返回宽度值
但是为了代码组织,我真的需要通过DialogFragment来实现这一点
任何关于我在这里做错了什么的帮助或指针都会非常有用。要在
对话框片段中获取子线性布局的宽度,您可以使用OnSowlistener
和getMeasuredWidth()
:
这适用于宽度设置为match\u parent
或wrap\u content
的子视图
(仅为普通视图
和线性布局
)
请注意,在我的测试中,我没有在onCreateDialog()
中调用CurrentDialog.show()
,而是在活动中使用了以下行来显示对话框:
DialogFragment mDialog = DF.newInstance("x", "yz");
mDialog.show(getSupportFragmentManager(), "myCurrentDialog");
要在对话框fragment
中获取子线性布局的宽度,您可以将OnShowListener
与getMeasuredWidth()
一起使用:
这适用于宽度设置为match\u parent
或wrap\u content
的子视图
(仅为普通视图
和线性布局
)
请注意,在我的测试中,我没有在onCreateDialog()
中调用CurrentDialog.show()
,而是在活动中使用了以下行来显示对话框:
DialogFragment mDialog = DF.newInstance("x", "yz");
mDialog.show(getSupportFragmentManager(), "myCurrentDialog");
我已经试过了。我甚至在该子视图(fieldForWidth)上的“onclick listener”设置中尝试了这一点。如果我使用TextView在该子视图中添加一些文本,那么我会得到一些宽度,但这与文本的宽度相同。所以我发现,当这个对话框是从DialogFragment中创建的时,宽度是不可测量的。有什么进一步的建议吗?@StackCoder-可能是调用“show()”太早导致的问题?请看一看我编辑过的答案——这次,我在发布之前测试了我的建议,它至少在Android Studio emulator(AppCompatActivity,Lollipop)中起了作用。我已经尝试过了。我甚至在该子视图(fieldForWidth)上的“onclick listener”设置中尝试了这一点。如果我使用TextView在该子视图中添加一些文本,那么我会得到一些宽度,但这与文本的宽度相同。所以我发现,当这个对话框是从DialogFragment中创建的时,宽度是不可测量的。有什么进一步的建议吗?@StackCoder-可能是调用“show()”太早导致的问题?请看一下我编辑过的答案——这次,我在发布之前测试了我的建议,它至少在Android Studio emulator(AppCompatActivity,Lollipop)中起作用