【问题标题】:Android : Using layoutinflater for custom layout dynamicallyAndroid:使用 layoutinflater 动态自定义布局
【发布时间】:2014-02-21 21:41:35
【问题描述】:

我正在做一个视频会议应用程序,其中我有一个框架布局,它将具有用于呈现视频的表面视图和用于结束该参与者呼叫的按钮。

由于可以动态添加参与者,因此当请求创建新用户时,我必须扩展此布局。

这个膨胀的布局将被添加到 tablelayout 中,这样我就可以容纳许多我想要的布局/用户。

我正在像这样膨胀 FrameLayout:

participantHolder.mParticipantFrameLayout = (FrameLayout)mInflater.inflate(R.layout.generic_paritcipant_layout, null);

我正在使用此布局中获取表面视图按钮:

//get the Dropuser button
participantHolder.mParticipantDropButton = (ImageButton)mFrameLayt.findViewById(R.id.ParticipantDropButton);
//get the surfaceview
participantHolder.mParticipantSurfaceView = (SurfaceView)mFrameLayt.findViewById(R.id.ParticipantSurfaceView);

并通过设置布局参数将布局添加到表格行中。

TableRow.LayoutParams PreviewLayoutParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT);
PreviewLayoutParams.weight = 1;
CurrentParticpant.mParticipantUIHolder.mParticipantFrameLayout.setLayoutParams(PreviewLayoutParams);
CurrentParticpant.mParticipantUIHolder.mParticipantFrameLayout.invalidate();
mTableRowOne.addView(CurrentParticpant.mParticipantUIHolder.mParticipantFrameLayout);
mTableRowOne.invalidate();
mTableLayoutReference.invalidate();

但我根本看不到视图/布局,如果我做错了什么请告诉我

【问题讨论】:

  • 你的mParticipantDropButtonmParticipantSurfaceView不应该在mParticipantFrameLayout里面吗?
  • mFrameLayt 这里什么都不是,但我将它称为 mFrameLayt =参与者Holder.mParticipantFrameLayout;两者都一样

标签: android android-layout


【解决方案1】:

我认为问题出在这里:

TableRow.LayoutParams PreviewLayoutParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT);

第一个参数表示宽度,您将其设置为 0。请尝试以下操作:

TableRow.LayoutParams PreviewLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1);

使用这样的构造函数你不需要添加PreviewLayoutParams.weight = 1; http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#TableRow.LayoutParams%28int,%20int,%20float%29

【讨论】:

  • 我认为问题不在于因为我给出的 weight=1 这意味着整个布局宽度空间。
  • 嗯,不,我将宽度设为 0,因为我的表格行方向是水平的,所以当我在其中添加一个布局时,它应该同样对齐 2 个布局,问题仍然存在。 :(
  • 如果您删除CurrentParticpant.mParticipantUIHolder.mParticipantFrameLayout.setLayoutParams(PreviewLayoutParams); 会怎样?我认为addView() 之后对invalidate() 的调用也可以删除
猜你喜欢
  • 2015-07-07
  • 2012-08-12
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 2017-05-29
  • 2014-06-25
相关资源
最近更新 更多