我需要将屏幕内容移到左侧,这样我就可以为右侧的幻灯片菜单腾出空间 这是代码: // modify content layout params try { content = ((LinearLayout) act.findViewById(android.R.id.content) .getParent()); } catch (Cl
这是代码:
// modify content layout params try { content = ((LinearLayout) act.findViewById(android.R.id.content) .getParent()); } catch (ClassCastException e) { /* * When there is no title bar * (android:theme="@android:style/Theme.NoTitleBar"), the * android.R.id.content FrameLayout is directly attached to the * DecorView, without the intermediate LinearLayout that holds the * titlebar plus content. */ content = (FrameLayout) act.findViewById(android.R.id.content); } FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content .getLayoutParams(); pr.rightMargin = menuSize; content.setLayoutParams(pr); // add the slide menu to parent parent = (FrameLayout) content.getParent(); try { parent = (FrameLayout) content.getParent(); } catch (ClassCastException e) { /* * Most probably a LinearLayout, at least on Galaxy S3. */ LinearLayout realParent = (LinearLayout) content.getParent(); parent = new FrameLayout(act); realParent.addView(parent, 0); // add FrameLayout to real parent of // content realParent.removeView(content); // remove content from real parent parent.addView(content); // add content to FrameLayout } LayoutInflater inflater = (LayoutInflater) act .getSystemService(Context.LAYOUT_INFLATER_SERVICE); menu = inflater.inflate(R.layout.slidemenu, null); FrameLayout.LayoutParams lays = new FrameLayout.LayoutParams(menuSize, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.RIGHT); lays.setMargins(0, statusHeight, 0, 0); menu.setLayoutParams(lays); parent.addView(menu);
但我得到的是:
内容只是调整大小以适应屏幕,我该如何使这项工作?
顺便说一句,我不能修改内容布局它的相对布局与surfaceview,但它应该适用于任何布局,因为我修改DecorView是顶视图容器
但是有关如何在菜单中实现幻灯片的精彩教程,请参阅此处: –
http://android.cyrilmottier.com/?p=658
为了增加进一步的帮助,这里的布局实现了我认为您所追求的目标: –
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/blue" > </LinearLayout> <LinearLayout android:layout_width="200dip" android:layout_height="match_parent" android:layout_marginLeft="-100dip" android:background="@color/grey_divider" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
这将导致第二个LinearLayout出现在屏幕左侧50%的位置.对于您正在移动的LinearLayout,需要一个绝对宽度.但是您可以在xml中将其定义为FILL_PARENT,然后在第一次将边距设置为负值时获取宽度并将其设置为代码中的绝对值.
希望这可以帮助.