[英]Programmatically set layout_gravity for a FrameLayout's Child?
This seems like it should be trivial, but I can't figure out how to set the layout_gravity for a FrameLayout's child programmatically for the life of me. 这似乎应该是微不足道的,但我无法弄清楚如何为我的生活中的编程方式设置FrameLayout的子项的layout_gravity。
There's a ton of similar questions on SO, but after trying a good 10 of them, none of the setGravity, new FrameLayout.LayoutParams, etc. solutions seem to work. 在SO上有很多类似的问题,但在尝试了其中的10个之后,setGravity,新的FrameLayout.LayoutParams等解决方案似乎都无法正常工作。
Below is a simplified version of what I'm trying to achieve. 下面是我想要实现的简化版本。
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextureView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</FrameLayout>
However, for external reasons, I'm adding the TextureView to the FrameLayout programatically. 但是,出于外部原因,我正在以编程方式将TextureView添加到FrameLayout。
mCameraPreview = new CameraPreview(this, recordMode);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview_wrapper);
preview.addView(mCameraPreview);
When I try to set the layout_gravity on the mCameraPreview, it only gives me an option for gravity, which I don't want. 当我尝试在mCameraPreview上设置layout_gravity时,它只给我一个重力选项,这是我不想要的。
Is there something I'm missing here? 这里有什么我想念的吗?
The layout_gravity
attribute lands on the FrameLayout.LayoutParams
, not on the view itself. layout_gravity
属性落在FrameLayout.LayoutParams
,而不是放在视图本身上。 You'll need something like: 你需要这样的东西:
mCameraPreview = new CameraPreview(this, recordMode);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview_wrapper);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT,
Gravity.BOTTOM);
preview.addView(mCameraPreview, params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.