我有一个导航抽屉,左侧是左侧导航抽屉,包含TextView" detailsTxt "。这包含动态文本,我希望在用户打开抽屉时调整导航抽屉的大小以适应TextView。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout mlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/app_bg">
<LinearLayout
android:id="@+id/topNavigator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/toolbar_white_space">
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:text="Ayman Salah"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold|italic"
android:layout_marginTop="15dp" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<!-- drawer layout -->
<LinearLayout
android:layout_width="450dp"
android:id="@+id/leftBar"
android:layout_gravity="start"
android:layout_height="fill_parent"
android:gravity="start|left">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nbackground">
<TextView
android:id="@+id/detailsTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="20sp"
android:text="string/sample_details"
android:textColor="@android:color/widget_edittext_dark" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
为抽屉添加侦听器
打开/关闭活动
和
在onDrawerOpen()
中添加此代码,LinearLayout mDrawerListView = (LinearLayout) findViewById(R.id.leftBar);
mDrawerListView.post(new Runnable() {
@Override
public void run() {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
params.width = (int) 100; // for example
mDrawerListView.setLayoutParams(params);
}
});
:
layout_width="0dp"
并在xml文件中设置{{1}}
答案 1 :(得分:0)
尝试初始化零宽度的导航绘图,即android:layout_width="0dp"
,然后在onCreate(Bundle savedInstanceState)
中执行下面的代码段。
// Programatically assigns a custom width to a DrawerLayout.
this.getDrawerLayout().addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
/** Fire-and-forget programmatic width implementation for the DrawerLayout. */
@Override public final void onDrawerSlide(final View pDrawerView, final float pSlideOffset) {
// Recalculate the new LayoutParams.
pDrawerView.setLayoutParams(this.getLayoutParams((DrawerLayout.LayoutParams)pDrawerView.getLayoutParams()));
// Stop listening for future events, once we've configured the size once. (We know the DrawerLayout is the direct parent of the DrawerView.)
((DrawerLayout)pDrawerView.getParent()).removeDrawerListener(this);
}
/** A method used to define the new LayoutParams of the DrawerView. */
private final DrawerLayout.LayoutParams getLayoutParams(final DrawerLayout.LayoutParams pLayoutParams) {
/** TODO: Compute your width here! (i.e pLayoutParams.width = X) **/
// Return the LayoutParams.
return pLayoutParams;
}
});