我在我的活动中使用SlidingPaneLayout:
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myslidingpanelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- menu left -->
<LinearLayout
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#8d305f"
android:orientation="vertical" >
...
</LineareLayout>
<!-- main page right-->
<LinearLayout
android:id="@+id/right_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical" >
...
</LineareLayout>
</android.support.v4.widget.SlidingPaneLayout>
我希望菜单覆盖页面的3/4,我希望它在所有的手机上工作,所以我不能把例如
android:layout_width="300dp"
我想计算屏幕宽度并将其设置为左侧窗格
感谢您的帮助
发布于 2013-07-10 14:10:58
感谢你们,我找到了这个答案,它对我很有效:
int width;
int height;
if (android.os.Build.VERSION.SDK_INT >= 13){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}else {
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth(); // deprecated
height = display.getHeight(); // deprecated
}
if(width>0&&height>0){
LinearLayout layout = (LinearLayout)findViewById(R.id.menu);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = height;
params.width = width*3/4;
}
发布于 2013-07-10 13:41:12
只需在文档中查找滑动窗格,看起来它的功能就像一个线性布局,并且可以使用
layout_weight
参数来设置基于百分比的宽度,因为父视图组为match_parent
在3/4 = 75%的情况下,可以
android:layout_weight="0.75"
来自android文档http://developer.android.com/reference/android/support/v4/widget/SlidingPaneLayout.html
与LinearLayout类似,SlidingPaneLayout支持在子视图上使用布局参数layout_weight来确定在测量完成后如何划分剩余空间。它只与宽度相关。当视图不重叠时,weight的行为就像在LinearLayout中一样。
当视图确实重叠时,可滑动窗格上的权重指示窗格的大小应调整为填充关闭状态下的所有可用空间。覆盖的窗格上的权重表示窗格的大小应调整为填充所有可用空间,但用户可以用来抓取可滑动视图并将其拉回关闭状态的最小小条带除外。
和来自LinearLayout文档的http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
注意:您将最终将layout_width参数设置为0dp,因为视图组实际上将使用权重来布局子项
发布于 2013-07-10 13:44:32
除了Selecsosi的答案是正确的,还有我写的this view总是将第二项显示为窗格(忽略默认的并排显示行为)。顾名思义,它可以环绕滑动视图。
你可以通过使用大量的@dimen资源并基于swXXXdp-(port|land)
切换它们,或者只是在运行时设置滑动视图的宽度来实现你想要的行为(我非常确定你也可以用默认布局来做这件事)。
https://stackoverflow.com/questions/17571908
复制