动态重新定位布局
给定下面的 main.xml 布局,是否可以在运行时更改 FrameLayout 的“layout_gravity”并立即显示结果?
我的主要布局是一个 LinearLayout 和一个居中的 FrameLayout 子布局,它有两个子布局,一个 SurfaceView 和一个 ImageView (位于框架布局的底部)。
我有一个菜单,让用户可以选择重新定位框架布局,这应该通过将layout_gravity 更改为“顶部”、“底部”或“中心”来完成。
为了实现这一点,我使用此代码在运行时更改重力:
int mGravity = Gravity.TOP // gravity determined from menu selection
FrameLayout mFrameLayout = (FrameLayout)findViewById(R.id.my_framelayout);
((LinearLayout.LayoutParams) mFrameLayout.getLayoutParams()).gravity = mGravity;
但是,这不会立即起作用。我只有在用户单击另一个菜单项(将他们带到 Android 市场)后才使其工作。当用户退出市场并返回到我的应用程序时,才会根据用户之前选择的内容绘制新的布局。
我错过了一些简单的事情吗?如何在用户选择位置后立即强制重绘?我应该使什么无效?
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/my_framelayout"
android:layout_width="fill_parent"
android:layout_height="@dimen/surfaceview_default_h"
android:layout_gravity="center">
<SurfaceView
android:id="@+id/my_surfaceview"
android:layout_width="@dimen/surfaceview_default_w"
android:layout_height="@dimen/surfaceview_default_h">
</SurfaceView>
<ImageView
android:id="@+id/my_imageview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="@drawable/someimage"
android:layout_gravity="bottom">
</ImageView>
</FrameLayout>
</LinearLayout>
菜单.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/mnu_position"
android:title="@string/position">
<!-- Position submenu -->
<menu>
<group
android:id="@+id/grp_position"
android:menuCategory="container"
android:checkableBehavior="single">
<item
android:id="@+id/mnu_position_top"
android:title="@string/top">
</item>
<item
android:id="@+id/mnu_position_center"
android:title="@string/center"
android:checked="true">
</item>
<item
android:id="@+id/mnu_position_bottom"
android:title="@string/bottom">
</item>
</group>
</menu>
</item>
<item
android:id="@+id/mnu_android_market"
android:title="@string/market">
</item>
</menu>
Given the main.xml layout below, is it possible to change the "layout_gravity" of the FrameLayout at runtime with the result shown immediately?
My main layout is a LinearLayout and a centered FrameLayout child with two children, a SurfaceView and an ImageView (positioned at the bottom of the frame layout).
I have a menu, that gives the user the option to reposition the frame layout, which should be done by changing layout_gravity to "top", "bottom", or "center".
In order to accomplish this, I use this code to change the gravity at runtime:
int mGravity = Gravity.TOP // gravity determined from menu selection
FrameLayout mFrameLayout = (FrameLayout)findViewById(R.id.my_framelayout);
((LinearLayout.LayoutParams) mFrameLayout.getLayoutParams()).gravity = mGravity;
However, this does not work immediately. I have only gotten it to work after the user clicks the other menu item, which takes them to the Android Market. When the user backs out from the market and returns to my app, only then is the new layout drawn according to what the user previously selected.
Am I missing something simple? How can I force a redraw right after the user selects the position? What should I invalidate?
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/my_framelayout"
android:layout_width="fill_parent"
android:layout_height="@dimen/surfaceview_default_h"
android:layout_gravity="center">
<SurfaceView
android:id="@+id/my_surfaceview"
android:layout_width="@dimen/surfaceview_default_w"
android:layout_height="@dimen/surfaceview_default_h">
</SurfaceView>
<ImageView
android:id="@+id/my_imageview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="@drawable/someimage"
android:layout_gravity="bottom">
</ImageView>
</FrameLayout>
</LinearLayout>
menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/mnu_position"
android:title="@string/position">
<!-- Position submenu -->
<menu>
<group
android:id="@+id/grp_position"
android:menuCategory="container"
android:checkableBehavior="single">
<item
android:id="@+id/mnu_position_top"
android:title="@string/top">
</item>
<item
android:id="@+id/mnu_position_center"
android:title="@string/center"
android:checked="true">
</item>
<item
android:id="@+id/mnu_position_bottom"
android:title="@string/bottom">
</item>
</group>
</menu>
</item>
<item
android:id="@+id/mnu_android_market"
android:title="@string/market">
</item>
</menu>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加。
设置重力后
try adding
after you set the gravity.